Ghobs Ghobs - 1 year ago 64
Javascript Question

Referencing Parse.User in a background job

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

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

When I try to do this below, it gives me an error stating that
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

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
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

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

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

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download