i new sails , trying learn crud functions make first api, of ponzi coder tutorials, how create restful json crud api in sails scratch?
for update request, tried piece of code application "small",
module.exports = { update: function (req, res, next) { var criteria = {}; criteria = _.merge({}, req.params.all(), req.body); var id = req.param('id'); if (!id) { return res.badrequest('no id provided.'); } small.update(id, criteria, function (err, small) { if (err) return next(err); if (small.length == 0) return res.notfound(); res.json(small); }); } } but unable understand significance , role of criteria array , second argument update function
small.update(id, criteria, function (err, small) {...}); i got criteria specifying changes want update, how ?
somebody please me out better understanding of this.
first of criteria not array empty object.
as of see in code line
criteria = _.merge({}, req.params.all(), req.body);
the above line signifies merging conditions sent in both parameters in url , body of post command using lodash object.
req.params.all() gives collection of parameters culled (in order of precedence):
1.the route (e.g. id in /post/:id).
2.the request body
3.the query string
according sails documentation update orm command updates existing records in database match specified criteria. have created sample example take _id body of post command. criteria on here column going update particular id.
crud.update(id,criteria,function(err,sleep){ if(sleep.length == 0) return res.badrequest(); if(err) return res.badrequest(err); res.json(sleep); }); }, hope answers question.
Comments
Post a Comment