Parse platform 解析检索具有objectId的用户的云代码

Parse platform 解析检索具有objectId的用户的云代码,parse-platform,parse-cloud-code,Parse Platform,Parse Cloud Code,我正在尝试从objectId获取用户对象。我知道objectId是有效的。但是我可以让这个简单的查询工作。怎么了?用户在查询后仍然未定义 var getUserObject = function(userId){ Parse.Cloud.useMasterKey(); var user; var userQuery = new Parse.Query(Parse.User); userQuery.equalTo("objectId", userId);

我正在尝试从objectId获取用户对象。我知道objectId是有效的。但是我可以让这个简单的查询工作。怎么了?用户在查询后仍然未定义

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;
};

问题是解析查询是异步的。这意味着它将在查询有时间执行之前返回user(null)。无论您想对用户做什么,都需要将其放在成功的内部。希望我的解释能帮助你理解为什么它没有定义


调查。在第一次查询得到结果后,这是一种更好的调用方式。

使用承诺的快速云代码示例。我有一些文件在里面,希望你能看懂。如果你需要更多的帮助,请告诉我

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;
        }
    });
};

我试着从成功中回归,但没有成功。我将考虑查询是异步的这一事实;我没有意识到这一点。你能将你的其他功能添加到success中吗?我将尝试在成功案例中使用承诺继续。我不知道query.first()方法。谢谢你!Parse.Cloud.useMasterKey();已在解析服务器版本2.3.0(2016年12月7日)中被弃用。从那个版本开始,它是一个无操作(它什么也不做)。现在,您应该将{useMasterKey:true}可选参数插入到需要在代码中重写ACL或CLP的每个方法中。