猫鼬模型值和合并新对象在节点 JS 中不起作用



我是平均堆栈开发的初学者。我正在尝试从集合中获取记录并在记录中添加另外两个对象,但我无法合并这些对象。

我已经尝试了以下方法。没有获取我试图与"项目"对象合并的"图像"对象的值。它不是合并。

var _ = require('lodash');
project.findOne({_id:id}).exec(function (err, projectdetails) {
  if(err){
    res.send(err);
  }

            projectimage.find({projectId: projectdetails._id}, function(err, data){
              if(err){
                res.send(err);
              }

var projects = projectdetails;
var images = [
   {
     image_id : 1,
    "image_location" : "f1.js"
   },
    {
     image_id : 2,
    "image_location" : "f2.js"
   } 
];
 projects = _.extend({},projects,{images:images},{projectimage:data});
        res.send(projects);
            });

    });

获得的输出:

{
  "_id": "58da3c350a668f13f0d6dbcc",
  "name": "senthil",
  "designation": "CI",
  "berief_text": "welcome to senthil",
  "status": 1,
  "__v": 0
}

预期输出:

{
  "_id": "58da3c350a668f13f0d6dbcc",
  "name": "senthil",
  "designation": "CI",
  "berief_text": "welcome to senthil",
  "status": 1,
  "__v": 0,
  "images": [
    {
      "image_id": 1,
      "image_location": "f1.js"
    },
    {
      "image_id": 2,
      "image_location": "f2.js"
    }
  ],
  "projectimage": [
    {
      "_id": "58da3c350a668f13f0d6dbcd",
      "imageLocation": "uploads\project_images\1490697269435download.jpg",
      "projectId": "58da3c350a668f13f0d6dbcc",
      "__v": 0
    },
    {
      "_id": "58da3c350a668f13f0d6dbce",
      "imageLocation": "uploads\project_images\1490697269439download (3).jpg",
      "projectId": "58da3c350a668f13f0d6dbcc",
      "__v": 0
    }
  ]
}

尝试使用

JSON.parse(JSON.stringify(projects)) 
JSON.parse(JSON.stringify(images)) 

以前

projects = _.extend({},projects,{images:images},{projectimage:data});

相关内容

  • 没有找到相关文章

最新更新