Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 猫鼬填充多个字段_Javascript_Mongodb_Api_Mongoose - Fatal编程技术网

Javascript 猫鼬填充多个字段

Javascript 猫鼬填充多个字段,javascript,mongodb,api,mongoose,Javascript,Mongodb,Api,Mongoose,我试图像使用关系数据库一样使用Mongoose。我有一个任务模式,需要引用客户机、用户和注释模式 我已经找到了许多用另一个模式填充一个模式的例子,但是没有一个是用于填充多个模式的。我尝试了以下链接: 当我对该数据执行GET请求时,我得到以下信息: "tasks": [ { "_id": "5d6980d8459a8b9f0e133d04", "user": [ {

我试图像使用关系数据库一样使用Mongoose。我有一个任务模式,需要引用客户机、用户和注释模式

我已经找到了许多用另一个模式填充一个模式的例子,但是没有一个是用于填充多个模式的。我尝试了以下链接:

当我对该数据执行GET请求时,我得到以下信息:

"tasks": [
        {
            "_id": "5d6980d8459a8b9f0e133d04",
            "user": [
                {
                    "_id": "5d6afd8a101f355244adfd9a",
                    "firstName": "Dexter",
                    "lastName": "Morgan"
                }
            ],
            "assignDate": "2018-12-09T00:00:00.000Z",
            "assignedStatus": "true",
            "completionStatus": "in-progress",
            "description": "This client needs to be contacted for future sales"
        }
]

我还想获取客户端id和名称以及与此任务相关的所有注释。

请尝试以下代码:

findAll: function (req, res) {
    Task
      .find(req.query)
      .sort({ date: -1 })
      // populate associated user
      .populate({path : 'user' , select: '_id firstName lastName'}) 
      // populate associated client
      .populate({path : 'client' , select: '_id name'})
      // all notes for the task and when they were created
      .populate({path : 'note' , select : '_id content created_at'})
      .then(dbModel => {
        res.status(200).json({
          tasks: dbModel.map(model => {
            return {
              _id: model._id,
              user: model.user,
              client: model.client,
              assignDate: model.assignDate,
              assignedStatus: model.assignedStatus,
              completionStatus: model.completionStatus,
              description: model.description,
              note: model.note,
            };
          })
        })
      })
      .catch(err => res.status(422).json(err));
  }
findAll: function (req, res) {
    Task
      .find(req.query)
      .sort({ date: -1 })
      // populate associated user
      .populate({path : 'user' , select: '_id firstName lastName'}) 
      // populate associated client
      .populate({path : 'client' , select: '_id name'})
      // all notes for the task and when they were created
      .populate({path : 'note' , select : '_id content created_at'})
      .then(dbModel => {
        res.status(200).json({
          tasks: dbModel.map(model => {
            return {
              _id: model._id,
              user: model.user,
              client: model.client,
              assignDate: model.assignDate,
              assignedStatus: model.assignedStatus,
              completionStatus: model.completionStatus,
              description: model.description,
              note: model.note,
            };
          })
        })
      })
      .catch(err => res.status(422).json(err));
  }