0

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

2
  • I don't think your syntax used in consumption attribute of your model is supported by Waterline. it looks like a separate model having a One-to-Many relationship. Commented Apr 14, 2016 at 18:08
  • Humm...are you sure? Commented Apr 14, 2016 at 18:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.