【问题标题】:Is there any way to populate created data in sails js (mongodb)有没有办法在sails js(mongodb)中填充创建的数据
【发布时间】:2016-10-13 12:19:05
【问题描述】:

我有一个名为 Business 的模型,它有一个类型为 modal 的字段地址:地址, 我想在创建业务时发送带有业务数据的地址对象,而不是仅发送 id。 简单的创建方法只是返回创建的地址对象的 id 我想填充响应 这就是我使用这个的原因:

register: function(req, res) {
        const values = req.body;
        Business.create(values).then(function(response) {
            Business.findOne({ id: response.id }).populate('address').then(function(response) {
                res.ok(response);
            });
        });
    },

但我认为这不是正确的方法.. 我再次查询数据库以填充结果。这正是我想要的。 我想用这样的东西:

    register: function(req, res) {
            const values = req.body;
            Business.create(values).then(function(response).populate('address') {
                 res.ok(response);
            });
        },

或任何删除第二个查询到数据库的东西

我知道这很疯狂......但请帮助

模型是

module.exports = {
  schema: true,

  attributes: {
    name: {
      type: 'string'
    },

    description: {
      type: 'string'
    },

    address: {
      model: 'Address'
    },

    email: {
      type: 'email',
      unique: true
    },

    landline: {
      type: 'string'
    },

    logo: {
      model: 'Image'
    },

    status: {
      type: 'string',
      enum: ['open', 'closed'],
      defaultsTo: 'open'
    },

    uniqueBusinessName: {
      type: 'string',
      unique: true
    },
  },
};

【问题讨论】:

  • 该型号的过去代码,请。
  • @SkyQ 模态添加..

标签: node.js mongodb sails.js


【解决方案1】:

create 类方法不支持populate,所以你这样做的方式是正确的。如果您想将此作为一项功能提出建议,请查看 Sails.js 项目的 guidelines for proposing features and enhancements

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2021-08-03
    • 2019-11-24
    相关资源
    最近更新 更多