node.js - 有没有办法填充 sails js (mongodb) 中创建的数据

标签 node.js mongodb sails.js

我有一个名为 Business 的模型,它有一个模态类型的字段地址:Address, 我想在创建业务时发送包含业务数据的地址对象,而不仅仅是 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
    },
  },
};

最佳答案

create 类方法不支持populate,因此您执行此操作的方式是正确的。如果您想将此作为一项功能提出,请查看 Sails.js 项目的 guidelines for proposing features and enhancements !

关于node.js - 有没有办法填充 sails js (mongodb) 中创建的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39890110/

相关文章:

node.js - 在 mongoose 中填充 'find' 的 post hook middlewhere

mongodb - 查询数据以在AWS中实现最小延迟的最佳方法

node.js - Sails 与 Loopback API 后端

node.js - 在离线 RHEL 上安装 pm2 的确切命令是什么

javascript - Meteor JS Jade 模板无法按预期工作

xml - 尝试从 Node.js 获取信息 SOAP 时出错

node.js - 如何在 mongodb 中插入嵌套对象?

java - 限制结果中的字段

node.js - Sequelize 1.7.3 中的时间戳

kubernetes - Grunt watch 在 Kubernetes 中抛出同步 :dev not found error with Sails. js