lundi 29 décembre 2014

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.


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


}





Aucun commentaire:

Enregistrer un commentaire