Sailsjs更新模型Mongodb

时间:2016-06-21 13:52:21

标签: mongodb sails.js waterline

我有两个收藏品;用户和地址。地址集合与用户集合相关联。

    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传递的userId和地址信息,我想更新地址集合。我尝试了下面的代码片段,但似乎数据没有得到更新。

var address = {
  address: "123",
  city: "Test City",
  state: "NY",
  zip: "12345"
};

User.findOne({user:userId}).then(function(model){
Address.update({id:model.id}, address)});

我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

查看sails 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