1

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Borbea
  • 412
  • 6
  • 12

1 Answers1

4

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

Dave W.
  • 1,576
  • 2
  • 18
  • 29
  • Hi Dave... I am trying to get collection data in business logic but not getting any thing. What i am trying, I need count for numbers of records in collection and i need that count.. I tried count, find but not getting success. Can you please help me. – Sandip Armal Patil May 06 '14 at 08:44
  • @SandipArmalPatil I'd suggest posting a question over on the Kinvey forums [1] - that would be the best way to get help. And the more detail you can provide, the faster you'll get a response. For example, when you post your question, try including your code, and any error messages you see. [1] support.kinvey.com – Dave W. May 06 '14 at 17:02