Sailsjs 更新模型 Mongodb
Sailsjs Update model Mongodb
我有两个collection;用户和地址。地址 collection 与用户 collection.
有关联
module.exports = {
attributes: {
email: {
type: 'email',
required: true,
unique: true
},
cart: {
collection: 'cart',
via: 'user'
},
isAdmin : {
type : 'boolean',
defaultsTo : false
},
}
module.exports = {
attributes: {
user: {
model: 'user',
required: true
},
address: {
type: 'string',
},
addressAdditional: {
type: 'string',
},
city: {
type: 'string',
},
state: {
type: 'json',
},
zip: {
type: 'string',
},
}
我有从 UI 传递的用户 ID 和地址信息,我想更新地址 collection。我尝试了以下代码片段,但数据似乎没有更新。
var address = {
address: "123",
city: "Test City",
state: "NY",
zip: "12345"
};
User.findOne({user:userId}).then(function(model){
Address.update({id:model.id}, address)});
我做错了什么?谢谢
查看风帆 adapter.js javascript 后,一个调用 mongo.objectId 的函数创建了一个 ObjectId。
var addressObjectId = mongo.objectId(model.id);
Address.update({_id:addressObjectId}, address)})
https://github.com/balderdashy/sails-mongo/blob/master/lib/adapter.js#L266
我有两个collection;用户和地址。地址 collection 与用户 collection.
有关联 module.exports = {
attributes: {
email: {
type: 'email',
required: true,
unique: true
},
cart: {
collection: 'cart',
via: 'user'
},
isAdmin : {
type : 'boolean',
defaultsTo : false
},
}
module.exports = {
attributes: {
user: {
model: 'user',
required: true
},
address: {
type: 'string',
},
addressAdditional: {
type: 'string',
},
city: {
type: 'string',
},
state: {
type: 'json',
},
zip: {
type: 'string',
},
}
我有从 UI 传递的用户 ID 和地址信息,我想更新地址 collection。我尝试了以下代码片段,但数据似乎没有更新。
var address = {
address: "123",
city: "Test City",
state: "NY",
zip: "12345"
};
User.findOne({user:userId}).then(function(model){
Address.update({id:model.id}, address)});
我做错了什么?谢谢
查看风帆 adapter.js javascript 后,一个调用 mongo.objectId 的函数创建了一个 ObjectId。
var addressObjectId = mongo.objectId(model.id);
Address.update({_id:addressObjectId}, address)})
https://github.com/balderdashy/sails-mongo/blob/master/lib/adapter.js#L266