Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

I am trying to write a server script as suggested and have come up with this so far:

prevar dbId = request.get("dbId");
console.log("dbId = ", dbId);
var masterkey = request.get("masterkey");
console.log("masterkey = ", masterkey);
var userId = request.get("userId");
console.log("userId = ",userId);
var username = "admin";

try {
// or login with masterKey
var token = DatabaseUser.login(dbId, username, null, masterkey).sessionToken;
console.log("token = ",token);

//retrieve user
//var userData = DatabaseUser.retrieve(dbId, userId, true, token);
//console.log("userData = ",userData);

//update flag
var updateJSON = {"events_available":true};
var response = DatabaseUser.update(dbId, userId, updateJSON, token);
console.log("response = ",response);
} catch (err) {
console.log("error has occured: ", err.message);
}/pre

This works on test, The value response holds data that I would like to pass back to the rest service, How is this done?

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

Also Is it possible to cycle through the user collection inside the server code or do I need to still do that in client side. I would only have to make one server call if it can all be done server side, other wise I would need a call for each user.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

I have tried: Response.success(response); to send data back to the calling rest service but I get an error saying Response is undefined.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

Ok, I figured out from documentation that response is in lowercase, however, i still get response is undefined.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

Can I retrieve all records of the _user collection so that I can loop round them in the server code?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

cycle through all users in database

Hello Terry,

Sure, it is possible. Please look at here: https://devcenter.appery.io/documenta...

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

The documentation you pointed to is for client side code, but I would like to do it server side. I basically need to load an array of JSON objects, each object being a record from the database, so that I can loop through to perform some action on each record.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

I found a function in the server side api called Collection.query, but this says:

You don't have permission to access this collection. Please check that you are using the correct API to access this collection.

preconsole.log("latest script call");
var dbId = request.get("dbId");
console.log("dbId = ", dbId);
var masterkey = request.get("masterkey");
console.log("masterkey = ", masterkey);
var userId = request.get("userId");
console.log("userId = ",userId);
var username = "admin";
var collectionName = "_users";

try {
//login with masterKey
var token = DatabaseUser.login(dbId, username, null, masterkey).sessionToken;
console.log("token = ",token);

// get a list of user records
var userRecords = Collection.query(dbId, collectionName, null, token);
console.log("userRecords = ",userRecords);

//update flag
//var updateJSON = {"events_available":true};
//var response = DatabaseUser.update(dbId, userId, updateJSON, token);
//console.log("response = ",response);
//response.success(response);
} catch (err) {
console.log("error has occured: ", err.message);
//response.error(err.message);
}/pre

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

tried changing to this:

pre // get a list of user records
var userRecords = DatabaseUser.query(dbId, null, token);
console.log("userRecords = ",userRecords);/pre

now getting the following error:

Script FlagNewEvent: Error: Invalid object specified ( @ 32 : 18 ) - throw e;

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

cycle through all users in database

Any update on why I can'y use 'response.success, response.error and DatabaseUser.query'?

Return to “Issues”