yablokoff
yablokoff

Reputation: 165

How to create multiple objects in a loop in Parse.com SDK?

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!

Upvotes: 1

Views: 2084

Answers (2)

Zorayr
Zorayr

Reputation: 24902

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

Upvotes: 3

user3532239
user3532239

Reputation:

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.

Upvotes: 1

Related Questions