user3426711 user3426711 - 4 months ago 7
Node.js Question

Get inserted record in Waterline

How to get the inserted record in Waterline?

Note: I am using sails.js

My code :

record=MyModel.create({
...
}).exec(function createdFile(err, _record){
...
return _record;
});


record
is always
undefined
but it is inserted in the DB, I want to return the inserted record in json to the client after the insert. (
res.json({'message':jsonx(record)});
).

Thanks

Answer

Record will be undefined as you are probably using the value prior to the callback completing. You need to reference _record as this is the created record within the callback after the create. You will need to nest inside to use the returned record e.g. do res.view('page',{message: _record});

Example usage: https://github.com/balderdashy/sails-docs/blob/master/reference/waterline/models/create.md

record=MyModel.create({

        }).exec(function createdFile(err, _record){

          return res.json({message: _record});
        });
Comments