Search code examples
javascriptbusiness-logickinvey

Kinvey Business logic - example of saving new object on Collections (Javascript)


i need help with Kinvey Business logic. I want to save a new object using javascript in Business login(back-end side) but I can't find an example nowhere. I tried this, but doesn't work:

var model = new Kinvey.Backbone.Model({}, {
                 url: 'Notifications'
                    });
        var promise = model.save({}, {
            success: function(model, response, options) {
        logger.info("bam");
                }
                    });

If anyone has examples of saving objects on normal Collections and on User collections I would appreciate very much. Thank you.


Solution

  • You can try using the collectionAccess module available from within your BL script. The docs for it can be found here.

    For example, to save a new object into the notifications collection, you would:

    function onPreSave(req, res, modules) {
    
      var db = modules.collectionAccess,
          objectToSave = { foo: "bar" };
    
      db.collection('notifications').save(objectToSave, function(err, objectThatWasSaved) {
        if (err) {
          // do some error reporting here
        } else {
          // Hooray! It worked
          // !! Make sure to call res.complete or res.continue
          // !! to tell Kinvey you are done processing. Check
          // !! the docs I linked to for the details
          res.continue();
        }
      });
    
    }
    

    Full disclosure: I'm a developer at Kinvey