我有这个数组
[
{id: "1280-00-477-5893-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514575144592", name: "Cancelled -> Replaced by 1280005462575", price: "$324", imgsrc: "https://firebasestorage.googleapis.com/v0/b/honeyb…=media&token=3acd47c0-773a-48c1-8956-9a025769b91e", availableqty: "4", …}
{id: "1510-00-140-1627-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514546926962", name: "AIRPLANE UTILITY", price: "$890", imgsrc: "https://firebasestorage.googleapis.com/v0/b/honeyb…=media&token=01267792-8f7f-40a6-921b-699e4366e5cb", availableqty: "65", …}
]
我想使用每个对象的id并对firebase数据库运行它以检索更多数据。
$('.checkout').click(function(){
// is user authenticated
firebase.auth().onAuthStateChanged((user) => {
if (user) {
var userId = firebase.auth().currentUser.uid;
for (var i = 0; i < cartarry.length; i++) {
var id = cartarry[i].id;
var finalqty = cartarry[i].quantity;
var price = cartarry[i].price;
var itemref = Cataloguedatabase.ref('/Listings/' + id).once('value', function(snapshot) {
var listingdetail = snapshot.val();
// subtract ordered quantity from available quantity
var availableqty = parseInt(listingdetail.Ava_QTY);
var orderedqty = parseInt(finalqty);
var qtyleft = availableqty - orderedqty;
console.log("orderd qty ", orderedqty)
console.log("Available qty ", availableqty)
console.log("What is now left is ", qtyleft)
console.log("priceee is ", price)
})
}
} else {
window.location = "../html/auth.html"
}
})
})
代码有效但由于某种原因它只打印出price和orderedqty的最后一个数组值。例如,它打印出324美元和890美元,而不是打印890两次,也像数量一样。我做错了什么,我该如何解决这个问题?
答案 0 :(得分:0)
使用let
而非var
let id = cartarry[i].id;
let finalqty = cartarry[i].quantity;
let price = cartarry[i].price;