我有一个名为 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/