【问题标题】:How to store an array of nested objects using Sails.js with MongoDB?如何使用 Sails.js 和 MongoDB 存储嵌套对象数组?
【发布时间】:2016-04-14 17:53:22
【问题描述】:

我正在尝试将嵌套对象保存在sails.js 模型中。 看起来是这样的:

module.exports = {
  schema: true,
  attributes: {
      label: {
          type: 'string',
          required: true,
      },
      consumption: [{
        timestamp: {
          type: 'string',
          required: true,
        },
        value: {
          type: 'float',
          required: true,
        },
      }],
  }
};

要在数组中包含值,我正在执行以下操作(在控制器内部):

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);
    }
});

但是当 plug.save 被执行时,sails 会中断并说Error: Unknown rule: 0

我已经搜索了如何在sails.js 上存储对象数组,但没有找到任何可行的方法。

谁能帮忙?

谢谢

【问题讨论】:

  • 我认为 Waterline 不支持您在模型的 consumption 属性中使用的语法。它看起来像一个具有一对多关系的单独模型。
  • 嗯...你确定吗?

标签: json node.js sails.js waterline sails-mongo


【解决方案1】:

consumption 中使用的语法错误或至少没有记录。 Waterline 支持属性类型 jsonarray as documented 但您不能为它们定义模式。要定义架构,您必须在模型和消费模型之间使用One-to-Many relationship

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2012-07-23
    • 2018-06-22
    • 2021-06-25
    • 2017-06-26
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多