0

Every Parse Installation object instance in my Parse database has a pointer to a specific user. I have a background job that runs for every user, and what I want to do in this part of the background job is to set the respective user Installation's channel property to ["yesPush"], for push notification targeting purposes.

The way I figured I would do it is by querying for the specific Parse.Installation instance, and then setting its channels property. This doesn't seem to be working however. I'm trying to follow the guidelines in the Parse Cloud Code Docs, but it's either not the correct use case, or I'm not following it correctly.

Code:

var installationQuery = new Parse.Query(Parse.Installation);
installationQuery.equalTo('userId', parentUser);

installationQuery.find().then(function(results) {
  return Parse.Object.set('channels', ["yesPush"]);
});
Ghobs
  • 839
  • 2
  • 14
  • 32
  • Where is `parentUser` set? why are you using a `return` call inside your find? Why are you using `find` instead of `first` if you expect at most 1 match? Why aren't you calling `set()` on the actual object itself? Why aren't you calling `save()` or `saveInBackground()`? – Timothy Walters Aug 27 '14 at 18:26
  • `parentUser` is an argument called by the encompassing function, it's used in different parts of the fxn without any problems, so I don't think thats the issue. I thought a return had to be used there, and using `find` instead of `first` was a silly mistake. How would I call `set()` on the object? – Ghobs Aug 27 '14 at 20:08

1 Answers1

2

The way I would do it is as follows:

// for every User
var userQuery = new Parse.Query(Parse.User);
userQuery.each(function(user) {
  var installationQuery = new Parse.Query(Parse.Installation);
  installationQuery.equalTo('userId', user);

  return installationQuery.first().then(function(installation) {
    installation.set('channels', ['yesPush']);
    return installation.save();
  });
}).then(function() {
  status.success();
}, function(error) {
  status.error(error.message);
});

The each() function is how you process large sets of data in a job. If you are performing other async tasks inside it you need to return their promise.

The first() function is much faster and easier if we only expect one record.

I am calling set() on the actual installation object returned by first().

I am returning the save() promise to allow promise chaining.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49