【问题标题】:How to declare array in sails model?如何在sails模型中声明数组?
【发布时间】:2015-04-12 06:00:08
【问题描述】:

我一直面临一个需要类似模型的数组的问题。我现在的模型是

module.exports = {
  attributes: {
    foodname:{
      type: 'string',
      unique: true,
      required: true,
    },
    foodreview:'string',
    foodlocation:'string',
    foodcategory:'string',
    foodupvote:'integer',
    fooddownvote:'integer',
    owner: {
      model: 'user'
    },
    Comments: {
      collection: 'comments',
      via: 'comment'
    }
  }
};

现在我想在其中添加投票数组,这样我就可以知道食物得到了多少赞成和反对。用户应该只为食物投票一次。所以我需要的是这样的

   module.exports = {
      attributes: {
        foodname:{
          type: 'string',
          unique: true,
          required: true,
        },
        foodreview:'string',
        foodlocation:'string',
        foodcategory:'string',
        foodupvote:'integer',
        fooddownvote:'integer',
        owner: {
          model: 'user'
        },
        Comments: {
          collection: 'comments',
          via: 'comment'
        },
        vote:{}
      }
    };

实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: arrays sails.js sails-mongo


    【解决方案1】:

    我们可以像这样使用 'array' 类型:

    module.exports = {
        attributes: {
        name: { type: 'string' },
            title: { type: 'string'},
            keywords: { type: 'array' },
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多