有时在计算某些项目之前返回总数。我试过了
if(i === receivedItems.length-1){
return callback(total)
}
解决这个问题,但我没有运气。
function getItemPrices(offer, callback) {
offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
if (err) {
return callback(-1)
} else {
let total = 0
for (let i = 0; i < receivedItems.length; i++) {
let url = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=" + receivedItems[i].appid + "&market_hash_name=" + receivedItems[i].market_hash_name + ""
url = url.replace(/#/, "%23")
request(url, function(error, response, body) {
if (error) {
console.log(error)
}
if (!error && response.statusCode == 200) {
total += parseFloat(JSON.parse(body).lowest_price.replace(/\$/, ""))
if (i === receivedItems.length - 1) {
return callback(total)
}
}
})
}
}
})
}