Srinivas
Srinivas

Reputation: 553

Sailsjs: Model is not inserting record on calling create method

I have a model Sample, When I try to to insert a record to this model Sample.create(req.body, res.send({done: 1})); or Sample.create(req.body).exec(res.send({done: 1}));I am not seeing any records beeing inserted on get request. I am executing this operation in SampleController.create I am getting response as {done: 1} without new record being inserted. As per sailsjs documentation, we can use Sample.save() but I am always getting exception no such method.

Note: Sample has only one field firstName. Using sailsjs 0.9.9

So what's wrong going here in order to insert new record?

Upvotes: 1

Views: 820

Answers (1)

sgress454
sgress454

Reputation: 24948

You're not calling Model.create correctly. The signature is either:

Model.create(data, callback)

or, better:

Model.create(data).exec(callback)

Where callback is a function with arguments err and createdModel. All together:

Sample.create(req.body).exec(function(err, sample) {

    if (err) {return res.serverError(err);}
    res.json(sample);

});

Docs are on the Sails website or on Github.

Upvotes: 2

Related Questions