I'm trying to save an nested object inside a sails.js model. This is how it looks like:
module.exports = {
schema: true,
attributes: {
label: {
type: 'string',
required: true,
},
consumption: [{
timestamp: {
type: 'string',
required: true,
},
value: {
type: 'float',
required: true,
},
}],
}
};
To include the values inside the array I'm doing the following (inside the controller):
if(!plug.consumption)
plug.consumption = [];
plug.consumption.push({
timestamp: req.param('timestamp'), /* Format: '2016-04-14T16:18:24.972Z' */
value: req.param('value'), /* Format: '6.5' */
});
plug.save(function (err){
if(err){
res.send("Error");
return next(err);
}
});
But when the plug.save is executed, sails breaks and says Error: Unknown rule: 0
I've searched how to store arrays of objects on sails.js but didn't find anything that would work.
Can anyone help?
Thanks
You syntax used in consumption
is wrong or at least not documented. Waterline supports attribute types json
and array
as documented but you can't define a schema for them. To define a schema you have to use a One-to-Many relationship between your model and a consumption model.