Javascript Mongoose查询内部。然后,尽管工作正常,但仍会收到警告和错误

Javascript Mongoose查询内部。然后,尽管工作正常,但仍会收到警告和错误,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,在阅读了关于是否可以在承诺句柄中执行mongoose查询的文档后,我有点困惑。在本例中,我尝试循环访问portfolio.coin中的一组对象,然后在另一个模型中查找它们的当前价格。有更好的方法吗?下面的代码可以工作,但我得到了未处理的PromiserRejection警告和documentnotfound错误 // @route: GET api/portfolio/ // @description: Get the latest portfolio with price chan

在阅读了关于是否可以在承诺句柄中执行mongoose查询的文档后,我有点困惑。在本例中,我尝试循环访问portfolio.coin中的一组对象,然后在另一个模型中查找它们的当前价格。有更好的方法吗?下面的代码可以工作,但我得到了未处理的PromiserRejection警告和documentnotfound错误

// @route:       GET api/portfolio/
// @description: Get the latest portfolio with price changes
// @access:      Private
router.get('/', passport.authenticate('jwt',
  { session: false }), (req, res) => {
    Portfolio.findOne({
      user: req.user.id
    }).then(portfolio => {
      if (portfolio) {
        // Loop over all the coins in the portfolio
        portfolio.coin.forEach((item, index) => {

          // In scope object 
          let details = {
            _id: item._id,
            exchange: item.exchange,
            currencyname: item.currencyname,
            currencyquantity: item.currencyquantity,
            currencypaidwith: item.currencypaidwith,
            pricepaid: item.pricepaid,
            currentprice: 0,
            pricedifference: 0
          };

          // For each coin find the market prices
          Market.findOne({
            exchange: item.exchange,
            coin_one: item.currencyname,
            coin_two: item.currencypaidwith
          }).then(result => {
            if (result) {

              // details
              details.currentprice = result.price;
              details.pricedifference = result.price; // TODO: percentage 
              portfolio.coin[index] = details;

              if (portfolio.coin.length >= index) {
                portfolio.save().then(portfolio => res.json(portfolio)).catch(err => res.status(404).json({ nodoc: "somethin went wrong" }));
              }
            }
          }).catch(err => res.status(404).json({ nodoc: "could not find a doc" }))
        });
      }
    })
  })

您需要在最后一行添加
。catch

// @route:       GET api/portfolio/
// @description: Get the latest portfolio with price changes
// @access:      Private
router.get('/', passport.authenticate('jwt',
{ session: false }), (req, res) => {
    Portfolio.findOne({
    user: req.user.id
    }).then(portfolio => {
    if (portfolio) {
        // Loop over all the coins in the portfolio
        portfolio.coin.forEach((item, index) => {

        // In scope object 
        let details = {
            _id: item._id,
            exchange: item.exchange,
            currencyname: item.currencyname,
            currencyquantity: item.currencyquantity,
            currencypaidwith: item.currencypaidwith,
            pricepaid: item.pricepaid,
            currentprice: 0,
            pricedifference: 0
        };

        // For each coin find the market prices
        Market.findOne({
            exchange: item.exchange,
            coin_one: item.currencyname,
            coin_two: item.currencypaidwith
        }).then(result => {
            if (result) {

            // details
            details.currentprice = result.price;
            details.pricedifference = result.price; // TODO: percentage 
            portfolio.coin[index] = details;

            if (portfolio.coin.length >= index) {
                portfolio.save().then(portfolio => res.json(portfolio)).catch(err => res.status(404).json({ nodoc: "somethin went wrong" }));
            }
            }
        }).catch(err => res.status(404).json({ nodoc: "could not find a doc" }))
        });
    }
    }).catch(err => res.status(404).json({ nodoc: "could not find a portfolio" }))
})

您需要在最后一行添加
。catch
您能更具体地说一下在哪里吗?是的,我在那里添加只是为了确认,我仍然会继续收到:未处理PromiserEjectionWarning:DocumentNotFoundError:找不到查询“{u id:5B1B1B1BFCDE8BC049DEA4B0,{u v:23}”的文档。如果我的数组有两个对象,并且它们都像正常情况一样得到更新,那么最后一个查询来自哪里,这很奇怪。我已经更新了答案,还需要为公文包添加一个catch。save()嘿,谢谢,我将接受作为答案。我还需要以一对一的方式显式映射对象中的字段。而不是完全取代。我收到一个新错误“error[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头”,但这是另一个问题,除非您知道可能的原因。感谢:if(portfolio.coin.length-1==index){portfolio.save().then(portfolio=>res.json(portfolio)).catch(err=>res.status(404).json({nodoc:“somethin出错了”}));}