【发布时间】:2018-08-11 21:37:08
【问题描述】:
所以我很难理解完成任务所需采取的步骤。我想获得所有用户的加密货币“监视列表”(我已经完成了)。然后根据监视列表中保存的内容从 coinmarketcap api 返回更新的数据。有人告诉我,我可以使用 Promise.all() 有效地完成此任务。我是否会从 mongodb 监视列表中查找/映射硬币的 id('bitcoin'),然后使用映射的 id 作为硬币参数运行 getcoins 函数?任何人都可以提供一些指导吗?
我试图做这样的事情,但没有奏效。也就是说 undefined 不是函数。
CryptoWatchlist.find()
.then(watchlists => watchlists.map(watchlist => watchlist.id))
.then(id => Promise.all(getCoins(id)))
/router/watchlist.js
router.get('/watchlist', (req, res) => {
CryptoWatchlist.find()
.then(watchlists =>
res.json(watchlists.map(watchlist => watchlist.serialize()))
)
.catch(err => {
console.error(err);
res.status(500).json({ message: 'Internal server error' });
});
});
/api.js
const fetch = require('node-fetch');
function getCoins(coin) {
return fetch(`https://api.coinmarketcap.com/v1/ticker/${coin}`).then(
response => {
return response.json();
}
);
}
module.exports = getCoins;
【问题讨论】:
-
您看到的确切错误是什么?
-
@randominstanceOfLivingThing 我目前看到的确切错误是 (node:17012) UnhandledPromiseRejectionWarning: Unhandled PromiseRejectionWarning: Unhandled Promise Rejection (rejection id: 2): TypeError: undefined is not a function (node:17012) [DEP0018] DeprecationWarning :不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
标签: javascript node.js express promise