如何使用nodejs在猫鼬的同一个集合中将整个数组从一个对象的数组推到同一集合中的另一个对象的数组?

时间:2020-09-03 13:43:28

标签: node.js arrays mongodb mongoose push

所以在这里,我试图通过将数组作为元素推入另一个对象的Addtaks中来实现此目的。但其给定错误为null。为什么?此处,在forEach循环内的if条件下,元素ID成功匹配。但是当pushine元素添加到Addtasks时,它将进入其他条件,并且在控制台中显示error = null。

猫鼬/快捷代码:

router.route('/addTasks-writers').get(function(req, res) {

  User.findOne({email:req.user.email},function (error, data) {
    if(data)
    {
    data.Addtasks.forEach(element => {
      if(element.id == req.query.id)
      {
        console.log("id matched for the Addtasks");
        console.log("element = "+ element);


    User.findOneAndUpdate({id:req.query.val}, {$push: {Addtasks: element}},function (error, data) {
        if(data)
        {
console.log("Success");
}

else
console.log(error);
})

})

正在推送的Addtasks数组的MongoDB jSON代码:

{
    "assignee" : "Assign",
    "displayLock" : "none",
    "displayDelete" : "inline",
    "commonID" : "gs76ztkxg04",
    "status" : "Requirement Completed",
    "Date" : "Thu Sep 03 2020 12:45:34 GMT+0530 (India Standard Time)",
    "exampleRadios" : "option1",
    "otherdetails" : "okay",
    "website" : "asad.com",
    "keywords" : "millionaire who earns in sleep",
    "words" : 12345,
    "topic" : "How to build the empire??",
    "_id" : ObjectId("5f509834df31590d88b02467"),
    "Bigpaths4Clients" : [],
    "Bigpaths" : [ 
        {
            "path" : "public\\files\\npm-debug.log",
            "name" : "npm-debug.log"
        }, 
        {
            "path" : "public\\files\\package.json",
            "name" : "package.json"
        }
    ]
}

0 个答案:

没有答案