使用 objectId 检索用户的解析云代码 [英] Parse Cloud Code retrieving a user with objectId

查看:29
本文介绍了使用 objectId 检索用户的解析云代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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('
User is: '+ user+'
');
    return user;
};

推荐答案

使用 Promise 的快速云代码示例.我在那里有一些文档,希望您可以关注.如果您需要更多帮助,请告诉我.

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

这篇关于使用 objectId 检索用户的解析云代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆