Page 1 of 1

Database Operations Command

Posted: Fri Oct 31, 2014 10:56 pm
by anton6984647

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?


Database Operations Command

Posted: Sat Nov 01, 2014 6:24 pm
by Illya Stepanov

Hi Anton,

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


Database Operations Command

Posted: Mon Nov 03, 2014 4:41 am
by Yurii Orishchuk

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.


Database Operations Command

Posted: Tue Nov 04, 2014 5:37 am
by anton6984647

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);


Database Operations Command

Posted: Wed Nov 05, 2014 1:42 am
by Yurii Orishchuk

Hi Anton,

Glad you get it work.

Thanks for your update.

Regards.