Page 1 of 1

Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 4:14 am
by James Glaubiger

How do you update a row on the Users Collection from a Server Code Script?

I have passed in the value I want to update as well as the user's token but when I do Collection.updateObject I keep getting an Error: Collection 'users' absent in database with id .....

There is no User.updateObject methods?


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 4:26 am
by Igor

Hello,

Why you don't want to use Database service for this task?
http://docs.appery.io/documentation/b...


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 4:37 am
by James Glaubiger

The data comes from an external application into the Server Code script. Can I pass parameters to a Database Service from the Server Code script? Is there an example?


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 4:52 am
by Igor

In this doc http://docs.appery.io/documentation/b... you can find an example how to update collection from Server Code.


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 6:09 am
by James Glaubiger

All I want to do is save one value passed into the Server Code script to a User's account row.

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

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

requestParams.userID /// Database User_id
requestParams.TrackingNumber /// Value I want to save to This User_id

I need to know if this is possible inside the Server Code script.... so far I have not been able to save anything to the Users table which is the default table, not a custom table or collection I have created.

I cannot even get an XmlHttpRequest because this code runs server side... so I can't do this

curl -X PUT
-H "X-Appery-Database-Id: 526fdbd8e4b07c3286b537f1"
-H "X-Appery-Session-Token: dc99a77b-ad80-4055-b986-6157b192aa69"
-d "{"TrackingNumber":"SOMEVALUE"}"
https://api.appery.io/rest/1/db/users...

Is there a HTTP client library available for use on the Server Code?


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 9:50 am
by Maryna Brodina

Hello! In server code to work with Users collection you should use XMLHttpRequest http://docs.appery.io/documentation/b.... REST API of Users table is here http://docs.appery.io/documentation/b...


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 10:16 am
by James Glaubiger

When I tried to use XmlHttpRequest I received an error stating that the variable was undefined. Do I need to manually include it somehow? XmlHttpRequest is implemented by the browser from what I read, and it appears that the Server Code script is not running in a browser?


Update Users Collection field from Server Code script?

Posted: Mon Nov 18, 2013 12:48 pm
by Maryna Brodina

Server Code script is not running in a browser. Most likely you have some error in server code. Please check example of using XMLHttpRequest here http://docs.appery.io/documentation/b...


Update Users Collection field from Server Code script?

Posted: Tue Apr 22, 2014 2:00 pm
by Ray Phelan

It would be nice to see an example of how this is done with XMLHttpRequest. I only find a GET method example in the documentation, it would be very helpful to see an example of the original question - how to use an update query (PUT method)


Update Users Collection field from Server Code script?

Posted: Wed Apr 23, 2014 1:59 am
by Igor

Hi Ray.

Here is a server code example how can you use XHR to modify user:
pre

var responseBody = {},
requestParams = {},
paramKeys = request&#46;keys();

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

var userObject = {"email": "hello@world&#46;com"};

var xmlHTTPParameters = {

Code: Select all

 "headers": { 
     &#47;&#47;Your DB id&#46; 
     "X-Appery-Database-Id": "52f32d06e4b0a25c11c89917", 

     &#47;&#47;Your session token&#46; You can only modify that user with whom logged in&#46; 
     "X-Appery-Session-Token": "ba504062-403a-4c02-b6b5-f34123d1d8cb", 

     "Content-Type": "application/json" 
 }, 

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

};

&#47;&#47;"53335c6ee4b07b0d80154c7e" - is user id&#46;
var XHRResponse = XHR&#46;send("PUT", "https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/users/53335c6ee4b07b0d80154c7e", xmlHTTPParameters);

console&#46;log(XHRResponse);

responseBody&#46;requestBody = request&#46;body();
responseBody&#46;requestParams = requestParams;
response&#46;success(XHRResponse, "application/json");
/pre
You can read about user editing here: http://docs.appery.io/documentation/b...

Here is about XHR in appery: http://docs.appery.io/documentation/b...

Also you can use this methods to work with users (more prefered):
http://docs.appery.io/documentation/b...