我试图将for循环返回的数字相加但由于某种原因,我无法将任何内容附加到我的局部变量并保存它们。有人可以指出我做错了什么,因为我是Javascript的新手吗?我正在尝试记录总价格,因为我逐个查询数据数组。感谢
Parse.Cloud.define('test', function(req, res) {
var items = [req.params.ids];
var totalPrice = 0.0;
var temp = [];
var temp2 = [];
for (var x = 0; x < items[0].length; x++) {
var items2 = Parse.Object.extend("Items");
var query = new Parse.Query(items2);
var totalp = [];
query.get(items[0][x], {
success: function(result) {
var ob = result.get("Price");
console.log(ob); // It logs the Float value and works fine until here
totalPrice = totalPrice + ob;
totalp.push(ob);
},
error: function(obj, err) {
res.error(err);
}
});
temp.push(totalp); //pushes empty array
console.log(totalp); //returns []
}
console.log(temp); //returns [ [], [] ]
res.success(totalPrice); // returns 0
});