anton6984647
Posts: 0
Joined: Mon Jul 14, 2014 3:53 pm

Database Operations Command

Hello,
I'm trying to update a row in a collection server-side and am using this code:

var XHRResponse = XHR.send("PUT", "URL_GOES_HERE", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey,
"Content-Type": "application/json"
},
"parameters": {
"where": "{'userid':'g45g4g45g'}",
"operations":"{'$set':{'user_team':'sdfsdfsdfsdf'}}"
}
});

It doesn't seem to be working, how should the operations command be used?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Database Operations Command

Hi Anton,

Your command seems to be OK - we will test this additionally and we let you know.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Database Operations Command

Hi Anton,

Here is worked code:

pre

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys&#46;length; key++) {
requestParams[paramKeys[key]] = request&#46;get(paramKeys[key]);
}

&#47;&#47;Settings:
var dbId = "52fd3d06xxxxxxxx9917";
var collectionName = "dogs";
var itemId = "53ba2599e4b09f5c7d50390f";

var bodyObject = {
"operations": '{"$set":{"toggle":true}}',
"where": '{"name":"one"}'
};

var XHRResponse = XHR&#46;send("PUT", "https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/collections/" + collectionName + "/" + itemId + "", {
"headers": {
"X-Appery-Database-Id": dbId,
"Content-Type": "application/json"
},

"body": JSON&#46;stringify(bodyObject)

});

if (XHRResponse&#46;status != 200) {
responseBody&#46;message = "message: " + XHRResponse&#46;body + "\ncode: " + XHRResponse&#46;status;
console&#46;log(XHRResponse&#46;body);
response&#46;error(responseBody, "application/json");
} else {
console&#46;log("Success: " + JSON&#46;stringify(XHRResponse&#46;body[0]));
response&#46;success();
}

/pre

Regards.

anton6984647
Posts: 0
Joined: Mon Jul 14, 2014 3:53 pm

Database Operations Command

Thanks, it's not quite what was needed, but I found another answer by you on the forum that works wonderfully (updating row without getting _id value first), code copied below:

var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
var dbId = "53eae11fe4b0d1037ddcbeed";
var bodyObject = {
"where": {"name":"joe"},
"operations": {"$set":{"value":"42_1"}}
};
var XHRResponse = XHR.send("PUT", "https://api.appery.io/rest/1/db/colle...", {
"headers": {
"X-Appery-Database-Id": dbId,
"Content-Type": "application/json"
},
//"body": {
// "where": '{"name":"joe"}',
// "operations": '{"$set":{"value":"42"}}'
//}
"body": JSON.stringify(bodyObject)
});
console.log(XHRResponse);

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Database Operations Command

Hi Anton,

Glad you get it work.

Thanks for your update.

Regards.

Return to “Issues”