0

I have two working Cloud Code functions: the first queries Parse.User and returns the objectId of record matching the username, the second queries Parse.Installation and looks returns the the objectId and deviceType of the record matching the pointer column to "user"(holds the users objectId).

In the interest of not having to continually make new HTTP calls in a VBA application I am trying put combined these two functions into one. I have written

Parse.Cloud.define ("lookupUser", function(request, response){
var email = request.params.email;

if(!email) {
   response.error("Missing parameter: email");
   return ;
}

Parse.Cloud.useMasterKey();
var objectId;
var installations = [];
var InstallationQuery = new Parse.Query(Parse.Installation);
var UserQuery = new Parse.Query(Parse.User);
UserQuery.equalTo("username", email);
UserQuery.first({
   success: function(user){
       objectId = user.id;
       InstallationQuery.equalTo("user", objectId);
       InstallationQuery.find({
           success: function(results){
               for(var i = 0; i < results.length; i++){
                   var object = result[i];
                   installations.push({
                       objectId: object.id,
                       deviceType: object.get("deviceType")
                   });
               }
           }
       }),
       response.success(objectId + installations);
   }
});
});

This successfully passes the objectId, unfortunately installations does not show up in the result and looks like this

{
"result":"b194RP5n89"
}

When I remove objectId from the response.succes like this

response.success(installations);

my result looks like this

{
 "result":[
  ]
}

I'm sure this is an issue with Async, but I'm operating outside of my depth.

Any help is deeply appreciated.

1 Answer 1

2

I recommend using Parse promises in serial. It will make your code a lot more readable. I'm still a little new to them, so this might not be exactly right...

...
Parse.Cloud.useMasterKey();
var objectId;
var installations = [];
var InstallationQuery = new Parse.Query(Parse.Installation);
var UserQuery = new Parse.Query(Parse.User);
UserQuery.equalTo("username", email);

UserQuery.first().then(function(user) {
    objectId = user.id;
    InstallationQuery.equalTo("user", objectId);
    return InstallationQuery.find()

}).then(function(result) {
    for(var i = 0; i < result.length; i++) {
        var object = result[i];
        installations.push({
            userId: object.get('user'),
            objectId: object.id,
            deviceType: object.get("deviceType")
        });
    }
    response.success(installations);

}, function(error) {
    response.error("Error...");
});

References for promises

Sign up to request clarification or add additional context in comments.

5 Comments

You've used result and results in your promise, otherwise great answer.
I tried this answer, fixing some of the minor syntax errors and still get the same result as the original code. ObjectId passes just fine, but the installations object is empty.
Is your user column an actual pointer column in the data browser or a string column that stores the user's objectId? If it'a pointer try modifying the query to: InstallationQuery.equalTo("user", user);
It's an actual pointer column. I changed InstallationQuery.equalTo as you suggested, that does work. The only issue I'm running into now is trying to pass the _User objectId now. response.success(objectId + installations); out puts the _User objectId and then [Object, object]. I tried installation.push({userId: objectId, But I get and error saying that "user" is not defined.
Found the solution. for(var i = 0; i < result.length; i++){ var object = result[i]; installations.push({ userId: object.get('user'), objectId: object.id, deviceType: object.get("deviceType") });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.