1

I'm trying to create several objects in a loop in Parse's Javascript SDK.

for (var i = 0; i < results.length; i++){
    var user = results[i],
        newPuzzle = new Puzzle();
    newPuzzle.set("userAsked", user);
    newPuzzle.save();
}

But it works only for several objects (from 2 to 5) and then falls to response. I found method Parse.Object.saveAll(list, options) but it doesn't work for creating AFAIK - only for updating.

I also used local function written on pure Node.js with Parse's master key - it can save objects in a loop and works perfectly. But I need working with filesystem and local JavaScript for me is a headache.

How can I create multiple objects in one request in Parse's SDK?

Thanks in advance!

4
  • 2
    How exactly is this failing? I believe this should work. Be aware that newPuzzle.save() is an asynchronous operation that does not complete instantaneous. You should pass in a success and an error callback function to a) wait until all save operations complete and b) be sure they complete successfully (or what the error is, if they fail). Also see the Parse documentation Commented Dec 4, 2013 at 14:08
  • @DanielBaulig Thanks for the comment. Saving doesn't fail, it works but only several times. And also callback doesn't log anything, not quite sure why. newPuzzle.save(null, {success: function(ob){console.log(obj);}}) Commented Dec 5, 2013 at 8:37
  • 1
    Save is async and returns a Promise. You need to collect the promises in an array and then wait for them to complete with a when I believe. Commented Dec 5, 2013 at 16:00
  • @yablokoff: So if you invoke newPuzzle.save(null, { success: function() { console.log('SUCCESS', arguments); ), error: function () { console.log('ERROR', arguments); } }) neither callback function will be eventually invoked for some save operations? If so, this is a bug and you should file it. Commented Dec 5, 2013 at 23:39

2 Answers 2

3

You want to use, saveAll function, see example below:

var TimelineEvent = Parse.Object.extend("Timeline");

exports.processNotifications = function (notifications, successCallback, failureCallback) {
  var timelineEvents = [];
  for (var i = 0; i < notifications.length; i++) {
    var notification = notifications[i];
    if (notification.hasOwnProperty("timelineEvent")) {
      var timelineEvent = new TimelineEvent();
      timelineEvent.set("eventNotificationKey", notification.notificationKey);
      timelineEvent.set("isDevelopmentAccount", notification.isDebugOnly);
      timelineEvent.set("eventName", notification.timelineEvent.name);
      timelineEvent.set("eventDescription", notification.timelineEvent.description);
      timelineEvent.set("eventValue", notification.timelineEvent.value);
      timelineEvent.set("channel", notification.channel);
      timelineEvents.push(timelineEvent);
    }
  }
  Parse.Object.saveAll(timelineEvents, {
    success:successCallback,
    error:failureCallback
  });
};
Sign up to request clarification or add additional context in comments.

Comments

1

Use saveAll function

https://www.parse.com/docs/js/api/symbols/Parse.Object.html#.saveAll

You will need to create an array of objects you wish to save first and pass as the first parameter.

Comments

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.