我有这个功能:
app.post('/list', function(req,res){
var imagelist=[]
var list=req.body.list
console.log("posted this: "+list)
list=list.split("\n")
console.log("starting for each")
list.forEach(function(card) {
mtg.card.where({name:card}).then(cards=> {
var url =cards[0].imageUrl
if(url){
imagelist.push(cards[0].imageUrl)
} else{
console.log("no such card")
}
console.log("get url: "+imagelist)
})
})
console.log("render"+imagelist)
res.render('list',{
list:list,
imagelist:imagelist[0]
})
})
此函数会生成此日志:
server start
posted this: squee
starting for each
render
get url: http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=370461&type=card
在填充图像列表之前调用渲染函数,我不知道为什么。
所有代码都可以在这里找到:https://github.com/JasonElting/mtgapp