简体   繁体   中英

Parse.com / Javascript - Save users objectid string as user pointer

How do I save a user pointer to an object when i have the object id of the user.

I am able to save the object to the class in Parse but the assignee is always 'Undefined' in Parse.

eg I have retrieved the user object and can get the username / object id etc through:

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

Which is called from my save task function below: (Note, I've logged all relevant fields and they return as expected.

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

}

So you should save a pointer to the user to the task.

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

By default, when fetching an object, related Parse.Objects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

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

This is explained here: https://parse.com/docs/js_guide#objects-pointers

The problem with your script is when you are querying in Parse it is done asynchronously so you can't return the user immediately. Instead you need to return the promise and then handle it when you call 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);
});

Take a look at this document on promise chaining for more information about promises:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM