gpt4 book ai didi

javascript - .then 内的 Mongoose 查询,尽管工作正常但仍收到警告和错误

转载 作者:行者123 更新时间:2023-12-03 01:42:28 26 4
gpt4 key购买 nike

在阅读了关于是否可以在 promise 句柄中执行 Mongoose 查询的文档后,我有点困惑。在本例中,我尝试循环portfolio.coin 中的一组对象,然后在另一个模型中查找它们的当前价格。有一个更好的方法吗?以下代码有效,但我收到 Unhandledpromiserejection 警告和 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" }))
})

关于javascript - .then 内的 Mongoose 查询,尽管工作正常但仍收到警告和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50776534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com