Parse Cloud Code retrieving a user with objectId
Asked Answered
C

2

8

I am trying to get the user object from objectId. I know the objectId is valid. But I can get this simple query to work. What is wrong with it? user is still undefined after the query.

var getUserObject = function(userId){
    Parse.Cloud.useMasterKey();
    var user;
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("objectId", userId);

    userQuery.first({
        success: function(userRetrieved){
            console.log('UserRetrieved is :' + userRetrieved.get("firstName"));
            user = userRetrieved;               
        }
    });
    console.log('\nUser is: '+ user+'\n');
    return user;
};
Cyclotron answered 9/10, 2014 at 19:56 Comment(0)
S
23

Quick cloud code example using promises. I've got some documentation in there I hope you can follow. If you need more help let me know.

Parse.Cloud.define("getUserId", function(request, response) 
{
    //Example where an objectId is passed to a cloud function.
    var id = request.params.objectId;

    //When getUser(id) is called a promise is returned. Notice the .then this means that once the promise is fulfilled it will continue. See getUser() function below.
    getUser(id).then
    (   
        //When the promise is fulfilled function(user) fires, and now we have our USER!
        function(user)
        {
            response.success(user);
        }
        ,
        function(error)
        {
            response.error(error);
        }
    );

});

function getUser(userId)
{
    Parse.Cloud.useMasterKey();
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("objectId", userId);

    //Here you aren't directly returning a user, but you are returning a function that will sometime in the future return a user. This is considered a promise.
    return userQuery.first
    ({
        success: function(userRetrieved)
        {
            //When the success method fires and you return userRetrieved you fulfill the above promise, and the userRetrieved continues up the chain.
            return userRetrieved;
        },
        error: function(error)
        {
            return error;
        }
    });
};
Sink answered 11/10, 2014 at 7:10 Comment(2)
Had no idea about the query.first() method. Thanks for this!Frannie
Parse.Cloud.useMasterKey(); has been deprecated in Parse Server version 2.3.0 (Dec 7, 2016). From that version on, it is a no-op (it does nothing). You should now insert the {useMasterKey:true} optional parameter to each of the methods that need to override the ACL or CLP in your code.Prig
F
0

The problem with this is that Parse queries are asynchronous. That means that it will return user (null) before the query has time to execute. Whatever you want to do with the user needs to be put inside of the success. Hopefully my explanation helps you understand why it's undefined.

Look into Promises. It's a nicer way of calling something after you get the result from the first query.

Fever answered 9/10, 2014 at 20:15 Comment(3)
I have tried returning from within success and it did not work. I will take into consideration the fact that queries are asynchronous; I had not realized that.Cyclotron
Can you add the rest of your functionality into the success?Fever
I will try using promises to proceed in success case.Cyclotron

© 2022 - 2024 — McMap. All rights reserved.