Ghobs Ghobs - 2 months ago 14
Javascript Question

Referencing Parse.User in a background job

What I want to do is create an MComparisonArray object with a

Name
property of
MatchCenter
, an
MCItems
array property equal to the
eBayResults
array, and to have the
parent
property of this object be a pointer to the
User
object that created it, that way each
User
has their own specific
MComparisonArray
object associated with their account.

When I try to do this below, it gives me an error stating that
User
is not defined, as I expected it would. I'm unsure of how to form the syntax to tell Parse to make the object's parent property be a pointer to the
User
.

I know that if I wanted to do this from within the iOS app, I would do
('parent', Parse.User.current())
, but there is no such thing as
Parse.User.current
in a background job.

Main Promise Chain:

Parse.Cloud.job("MatchCenterBackground", function(request, status) {
//code to setup usersQuery ...
var usersQuery = new Parse.Query(Parse.User);

return usersQuery.each(function (user) {
return processUser(user).then(function(eBayResults){
return matchCenterComparison(eBayResults);
});
});
});


Snippet from
matchCenterComparison
function:


var newMComparisonArray = new mComparisonArray();
newMComparisonArray.set('Name', 'MatchCenter');
newMComparisonArray.set('MCItems', eBayResults);
newMComparisonArray.set('parent', Parse.User);

// Save updated MComparisonArray
newMComparisonArray.save().then({
success: function() {
console.log('MComparisonArray successfully created!');
},
error: function() {
console.log('nah no MComparisonArray saving for you bro:' + error);
}
});

Answer

You have the user in your outer scope, but you aren't passing it into the function.

Change your calling code to pass the current user like this:

return matchCenterComparison(user, eBayResults);

Update your function definition to accept the user as the first parameter:

function matchCenterComparison(parentUser, eBayResults) {
    // ...
}

And then use that user for the parent:

newMComparisonArray.set('parent', parentUser);