我正在尝试解析地图功能中的数据

时间:2019-01-28 09:11:13

标签: javascript mongodb

下面是我的代码。我无法解析o / p。如何在异步和等待状态下解析。我正在尝试从其他集合中获取数据并将其传递给现有的经过过滤的集合

exports.listStatus = async (root, { _id, start, limit }) => {
  var user = await db.User.find({ _id: _id })    
    let list = user[0].savedUsers
    var blocked = user[0].blockedUsers
    var listAll = list.push(_id)
    let listOfStatus = await db.Status.find({ userId: { $in: list }, reportStatus: { $ne: _id }, _id: { $nin: blocked } });
    let array;
    let leng = listOfStatus.length
    let statusCommentsArray
    var arrayStatus = [];
    listOfStatus.forEach((status, key) => {
      let promise = new Promise((resolve, reject) => {
        var obj = {}
        var statusId = status._id
        var listOfLiked = status.like
        let likeCount = listOfLiked.length
        var sortedList = listOfLiked.sort({ createdDate: -1 })
        obj.likeCount = likeCount

        db.Comment.find({ statusId: statusId }, (err, statusCommentsList) => {
          var statusCommentsCount = statusCommentsList.length
          var sortedList = statusCommentsList.sort({ createdDate: -1 })
          if (statusCommentsCount >= 3) {
            statusCommentsArray = sortedList
          }
          else {    
            statusCommentsArray = sortedList.slice(0, 3)
          }
          obj.statusCommentsCount = statusCommentsCount
          obj.statusCommentsArray = statusCommentsArray
          arrayStatus.push(obj)
          leng--;
          if (leng == 0) {
            resolve(arrayStatus)
          }          
        })    
      })
      promise.then((response) => {
        return Promise.resolve(response)
      })
    })
}

0 个答案:

没有答案