【问题标题】:Sailsjs - Prevent non-model fileds to be saved in mongo documentSails Js - 防止非模型字段保存在 mongo 文档中
【发布时间】:2015-09-11 16:26:17
【问题描述】:

我最近开始使用 Sailsmongo。 我使用 Sails 蓝图来生成我的部分 api。 问题是,无论模型中定义的字段如何,我发送的请求正文都被保存到 mongo 集合中。

例如,假设我有以下 Event 模型:

module.exports = {

  attributes: {

    title: {
     type: 'string',
     required: true
    },
  } 
}

当我使用以下参数向 /event/ 端点发送 POST 请求时:

{"title":"Some Event", "random":"string"}

保存的 mongo 文档还包含 "random":"string" 值,即使它不是模型的一部分。

我试图想出一些通用方法在为所有模型创建之前删除非模型属性,但可能的解决方案似乎不正确和肮脏。

我错过了什么吗?

任何帮助将不胜感激!

【问题讨论】:

    标签: sails.js sails-mongo


    【解决方案1】:

    您可以在模型中使用schema 选项。只需将其添加到模型声明中即可。

    // api/models/Model.js
    module.exports = {
      schema: true,
      attributes: {
        title: {
          type: 'string',
          required: true
        }
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多