Reputation: 9435
While using sails 1.0 I run into a problem.
The sails documentation describes that the create
function's callback (or promise) has an argument describing the inserted record. In my case (see code below) this value is always "undefined". I can verify that the entry is however added to the database. (Both using postgresql and the localdiskdb). - So the model is set up correctly.
The route connects (correctly) to:
add: function (req, res) {
const params = req.allParams();
const {
event_id,
name,
date,
} = params;
const dat = {
name:name,
id:Number(event_id),
date_start:new Date(Number(date)),
};
console.log(dat);
console.log(typeof dat.location);
Event.create(dat)
.then(function (result){
console.log(result)
return res.ok();
})
.catch(function (err){
console.log(err);
return res.send(err);
});
}
So why is the result
in my callback undefined?
Upvotes: 0
Views: 463
Reputation: 1178
With Sails.js v1, you will have to include the .fetch() function to send back records that were updated/destroyed/created when performing an .update(), .create(), .createEach() or .destroy() query. Otherwise, no data will be returned (or if you are using callbacks, the second argument to the .exec() callback will be undefined.)
The official example:
var newUser = await User.create({ fullName: 'Alice McBailey' }).fetch();
For your code above, try:
Event.create(dat).fetch().exec(function(err, result){
if(err) {
console.log(err);
return res.send(err);
}
console.log(result)
return res.ok();
})
For more info, see the official sails documentation on .fetch() here.
Upvotes: 1