变量的值不变

时间:2018-02-20 15:19:51

标签: node.js variables woocommerce woocommerce-rest-api

所以这是我的代码:

  while (n !== -1){
var meta = result[n].meta_data;
var tradeurl = meta[0].value;
var orderid = result[0].id;
var billing = result[0].billing;
var name = billing.first_name +" "+ billing.last_name;
var orderitem = result[0].line_items;
var orderitemid = orderitem[0].id;
console.log("n = " + n);
console.log("Order ID : " + orderid);
console.log("Using tradeurl : " + tradeurl);
console.log("Buyer : " + name);
console.log("Order : " + orderitemid);
n= n-1 ;

}

这就是我在控制台中获得的内容:

Order ID : 2979
Using tradeurl : https://steamcommunity.com/tradeoffer/new/?partner=452464312&token=1m7Sxxxx
Buyer : devang yadav
Order : 1643
n = 2
Order ID : 2979
Using tradeurl : https://steamcommunity.com/tradeoffer/new/?partner=452464312&token=Gq27xxxx
Buyer : devang yadav
Order : 1643
n = 1
Order ID : 2979
Using tradeurl : https://steamcommunity.com/tradeoffer/new/?partner=387415982&token=UVZYxxxx
Buyer : devang yadav
Order : 1643
n = 0
Order ID : 2979
Using tradeurl : https://steamcommunity.com/tradeoffer/new/?partner=410734147&token=WIK-xxxx
Buyer : devang yadav
Order : 1643

所以我正在做的是获取API返回的数组的长度,当数组从0索引开始,然后尝试更改meta = result[n].meta_data;中的值以便获取时,从中减去一个数组新数据。但由于某种原因,它无法正常工作,n的价值正在发生变化,如控制台输出所示,但出于某种原因,新数据并未被拉动。同时我发布此信息时,我注意到tradeurl正在更新,但其他人都没有。我哪里错了?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

此处您传递"0"而不是n

var orderid = result[0].id;
var billing = result[0].billing;
var orderitem = result[0].line_items;
var orderitemid = orderitem[0].id;

替换为n

var orderid = result[n].id;
var billing = result[n].billing;
var orderitem = result[n].line_items;
var orderitemid = orderitem[n].id;