1

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.

e.g. 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);
        }
    });
}

}

1 Answer 1

2

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:

Sign up to request clarification or add additional context in comments.

11 Comments

This lets me save the object id as a string, but am looking to save it as a pointer. Or... should I just use the string? Re: the promise, i'll have to read up a bit more on that. I don't quite understand why it isn't working now - it all seems to be except for saving the user pointer.
The objectId is a string. If you look at the Parse data browser.
The object Id is most likely not saved against the task because it's not being returned from the getUserFromUsername due to promise issue.
Thanks for helping me with this Wayne. I am converting parts of my application from PHP to Javascript, and in PHP I could retrieve a Parse User object and save this as a User Pointer (e.g. in Parse the field was of type Pointer <_User>), but I can only do this for the currently signed in user by saving Parse.User.current(). Everything is returning as expected, and I can retrieve all the user data from the returned object (e.g. username, object id etc) but when I save it just won't do it.
After having a look, you can use pointers. I'll update my answer: parse.com/docs/js_guide#objects-pointers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.