Search code examples
javascriptfunctionparse-platformparameter-passingparse-cloud-code

Parse cloud code - query user issue in "normal" function


I cannot get cloud code to query a user when using a "standard" function...

If I define the function (as below), it works fine...

Parse.Cloud.define("findUser1", function(request, response){
   Parse.Cloud.useMasterKey();
   var query = new Parse.Query(Parse.User);
   query.equalTo("objectId", "2FSYI1hoJ8"); // "2FSYI1hoJ8" is the objectId of the User I am looking for
   query.first({
       success: function(user){
       response.success(user);
   },
   error: function(error) {
       console.error(error);
       response.error("An error occured while lookup the users objectid");
   }
   });
 });

In this version, the function will be called, but the query within will not...

function findThisUser(theObject){
    console.log("findThisUser has fired... " + theObject); //confirms "theObject" has been passed in
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("objectId", "2FSYI1hoJ8"); // "2FSYI1hoJ8" is the value of "theObject", just hard coded for testing
   query.first({
       success: function(users){
       console.log("the user is... " + users);
       // do needed functionality here
   },
       error: function(error) {
       console.error(error);
   }
   });
};

Cloud code does not allow global variables, and I do not see how to pass in a variable to a "defined" function from another one. This is crucial as an outside function must be called to run the required tasks on the returned user. (This happens elsewhere and has to happen AFTER everything else does. This is the confirmation, and is supposed to be used by other functions as well) All potential information found to date has not helped, and the only experience I have in server side javascript is what I have cobbled together from other cloud code...

Any ideas on what I am missing?


Solution

  • This link may help, i had similar issue yesterday and after moving the code a little, and having the response.success(user); in my function all worked fine.

    Parse Cloud Code retrieving a user with objectId

    Not your exact issue - but this may help.

    Heres is the code i use now:

    Parse.Cloud.define("getUserById", function (request, response) {
    //Example where an objectId is passed to a cloud function.
    var id = request.params.objectId;
    
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("ObjectId", id);
    
    query.first(
    {
        success: function(res) {
            response.success(res);
        },
        error: function(err) {
            response.error(err);
        }
    });
    

    });