Parse.com / Javascript - 将用户objectid字符串保存为用户指针

时间:2014-12-30 03:50:21

标签: javascript parse-platform

当我拥有用户的对象ID时,如何将用户指针保存到对象。

我能够在Parse中将对象保存到类中,但是Parse中的受理人始终是“Undefined”。

e.g。我检索了用户对象,可以通过以下方式获取用户名/对象ID等:

function getUserFromUsername(username) {

Parse.initialize("...", "...");

console.log('The username passed in is: ' + username);

var User = Parse.Object.extend("_User");
var query = new Parse.Query(User);
query.equalTo("username", username);

query.first({
     success : function(result) {
        // Do something with the returned Parse.Object values
            var userPointer = new Parse.User();
            userPointer = result;
            console.log(userPointer.get('username')); // this returns the correct username
            return userPointer;

    },
    error : function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
});
}

从我的保存任务功能调用以下内容:(注意,我已记录所有相关字段,并按预期返回。

function saveNewTask(clientName, taskTitle, taskDue, assigneeArray) {

Parse.initialize("...", "...");

var x;
for (x in assigneeArray) {

    var Task = Parse.Object.extend("Tasks");
    var task = new Task();

    task.set("title", taskTitle);
    task.set("date", taskDue);

    var thisAssignee = GetUserFromUsername(assigneeArray[x]);
    task.set('assignee', thisAssignee);

    task.save(null, {
        success : function(task) {
            // Execute any logic that should take place after the object is saved.
            console.log('New object created with objectId: ' + task.id);
        },
        error : function(gameScore, error) {
            // Execute any logic that should take place if the save fails.
            // error is a Parse.Error with an error code and message.
            console.log('Failed to create new object, with error code: ' + error.message);
        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

因此,您应该将指向用户的指针保存到任务中。

var Task = Parse.Object.extend("Tasks");
var task = new Task();

task.set("user", user);
task.set("title", "taskTitle");
task.set("date", taskDue);

task.save(null, {
    success : function(task) {
        // Execute any logic that should take place after the object is saved.
        console.log('New object created with objectId: ' + task.id);
    },
    error : function(gameScore, error) {
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and message.
        console.log('Failed to create new object, with error code: ' + error.message);
    }
});

默认情况下,在获取对象时,不会获取相关的Parse.Objects。这些物品'在获取值之前,无法检索这些值:

var user = task.get("user");
user.fetch({
  success: function(user) {
       //fetch user is here
  }
});

这在此解释:https://parse.com/docs/js_guide#objects-pointers

您的脚本问题在于您在Parse中查询它是异步完成的,因此您无法立即返回用户。相反,您需要返回promise,然后在调用getUserFromUsername:

时处理它
function getUserFromUsername(username) {
    var User = Parse.Object.extend("_User");
    var query = new Parse.Query(User);
    query.equalTo("username", username);
    return query.first();
}

getUserFromUsername('testUsername').then(function(result) {
    //use User here
}, function(error) {
    alert("Error: " + error.code + " " + error.message);
});

请查看this document on promise chaining以获取有关承诺的更多信息: