猫鼬更新'不能使用零件(..)遍历元素 [英] Mongoose update 'cannot use the part (..) to traverse the element

查看:84
本文介绍了猫鼬更新'不能使用零件(..)遍历元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常烦人的问题,我无法使用猫鼬更新任何内容.使用起来真的很令人沮丧,而且文档根本​​没有帮助.

I have this really annoying issue where i can't update anything using mongoose. It's really frustrating to use, and the documentation is not helping at all.

我有这个模式:

var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String,
    },
    devices : [{
      id : String,
      name : String
    }]
});

这是我要将设备添加到数组devices的代码:

And this is the code where i want to add a device to the array devices :

function updateDeviceList(user, deviceID, deviceName)
{
  User.update({ 'local.email' : user}, 
  { $set: {'devices.id' : deviceID, 'devices.name' : deviceName}}, 
  function(err, response)
  {
    if(err)
    {
      console.log("Update device error", err);
    }
    else {
      console.log("Update device OK");
    }
  });
}

这时我得到了错误: errmsg: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }

At this point i get the error: errmsg: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }

我没有找到解释为什么发生这种情况的原因.我不得不提到该文档(数据库中几乎只有一个文档),是这样的:

I didn't manage to find an explanation to why this is happening. I have to mention that the document (there is pretty much only one document in the database), is this one:

{
    "_id": {
        "$oid": "5585a196fe11b21100635c74"
    },
    "devices": [],
    "local": {
        "password": "$2a$10$7hXVHw7izcYlqbD6xe/te.0w2zucZ7lA007g9kXdoIMPhZhRyCIru",
        "email": "a@a.a"
    },
    "__v": 0
}

推荐答案

使用$ push或其他数组更新运算符将元素添加到数组中.有关详细信息,请参考 http://docs.mongodb.org/手册/参考/操作员/更新/推送/#up._S_push

Use the $push or other array update operators to add elements to an array. For details, refer http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push

这篇关于猫鼬更新'不能使用零件(..)遍历元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆