【问题标题】:Sailsjs Update model MongodbSailsjs 更新模型 Mongodb
【发布时间】:2016-06-21 13:52:20
【问题描述】:

我有两个系列;用户和地址。地址集合与用户集合有关联。

    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 和地址信息,我想更新地址集合。我尝试了以下 sn-p,但数据似乎没有更新。

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

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

我做错了什么?谢谢

【问题讨论】:

  • 地址集合中文档的字段“id”是否为“ObjectId”类型?如果是这样,请将此 id 转换为“ObjectId”。

标签: mongodb sails.js waterline


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多