【问题标题】:Can't manipulate mongoose query result - promise undefined无法操纵 mongoose 查询结果 - 承诺未定义
【发布时间】:2020-12-23 05:20:06
【问题描述】:

我是猫鼬和承诺的新手,所以我可能不太清楚,事情是这样的:

我正在编写我的应用程序 (MEAN),其中我的控制器中堆叠了一些 Web 服务来检索和获取内容。我举个例子吧。

exports.allowanceCheck = (req, res) => {

  let payload = req.body.excelparam
  let day = getDate(payload[0].data_accesso,1)
  var dummyVar

   return AccessList.aggregate([
    {'$match' : {'id': day}},
    {'$unwind' : '$employees'},
    {
      $group: {
        _id: '$id',
        count: { $sum: 1 }
      }
    }
   ])
  .exec()
  .then((list) => {
    console.log(list) //not working
    res.json(list)
    dummyVar = list
  })
  .catch((err) => {
    res.status(500).send(err)
    
  })

}
console.log(list) //not working
console.log(dummyVar) //not working

Web 服务运行良好,客户端得到他的​​响应 JSON:

[
    {
        "_id": "200830",
        "count": 2
    }
]

我现在遇到的问题是,如果我想对这个“列表”JSON 数据做一些事情,我会得到一个错误,我试图操纵 {undefined} 东西。 (例如,你看到的任何地方//不工作 - 可能在其他任何地方) 所以我只能将数据发回,我无法操作任何东西。

我尝试保存承诺并访问它,但没有成功。

我做错了什么?

谢谢! :)

【问题讨论】:

    标签: node.js express mongoose promise mean-stack


    【解决方案1】:

    您可以尝试以下方法以更好地理解promise

        exports.allowanceCheck = (req, res) => {
    
        let payload = req.body.excelparam
        let day = getDate(payload[0].data_accesso, 1)
    
        functionAllowanceCheck(day).then((listResponse) => {
            console.log(listResponse) //working
            res.json(listResponse)
        }).catch((listError) => {
            res.status(500).send(listError)
        })
    
    }
    
    const functionAllowanceCheck = (day) => {
        return new Promise((resolve, reject) => {
            AccessList.aggregate([
                { '$match': { 'id': day } },
                { '$unwind': '$employees' },
                {
                    $group: {
                        _id: '$id',
                        count: { $sum: 1 }
                    }
                }
            ]).exec((err, list) => {
                if (err) {
                    reject(err)
                } else {
                    resolve(list)
                }
            })
        })
      }
    

    【讨论】:

    • 介绍Explicit Promise Anti-pattern 更多的是阻碍而不是帮助。
    • 谢谢!现在我想在 then 子句之外使用“count”值,因为我想保留结果,进行另一个查询,然后响应将所有数据附加到 WS 调用触发的相同响应中。策略应该是什么?仍然无法读取 dummyVar 值 :(
    • 你可以试试 let result = JSON.parse(JSON.stringify(listResponse)) 之后你就可以更新值了
    • @vishalpankhaniya 很遗憾不能,请在下面回复。有什么线索吗?
    【解决方案2】:

    @vishalpankhaniya 不幸的是 :( 在街区之外阅读它是不可能的。 我尝试在块内部和之前创建一个 var 并且它没有改变(让不应该在整个 permitCheck 中工作,对吧?)

    exports.allowanceCheck = (req, res) => { 
       ... 
       var lista = JSON.parse(JSON.stringify(listResponse)) 
       ... 
       }).catch((listError) => { 
          res.status(500).send(listError) 
       }) 
    console.log(lista) }
    

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 2018-08-17
      • 2019-10-21
      • 2018-03-18
      • 2021-11-01
      • 1970-01-01
      • 2019-08-15
      • 2019-02-06
      相关资源
      最近更新 更多