【发布时间】:2018-08-24 22:28:54
【问题描述】:
当我在请求中时,我正在尝试修改变量。但我不知道为什么它没有被修改。
const request = require("request");
var all = JSON.parse(body);
var steamplayer = all['response']['players']['player'][0];
var sgameh = "Nessun gioco";
request({
url: "https://example.com",
method : 'GET'
}, function (error, response, body){
var sgameall = JSON.parse(body);
var fgame = sgameall['response']['games'].filter(function(item) {
return item.appid == steamplayer.gameid;
});
sgameh = parseFloat(fgame[0].playtime_forever / 60).toFixed(2);
console.log(sgameh) // THIS WORKS, BUT IT ISN'T WHAT I WANT
})
console.log(sgameh) // SHOULD RETURN A NUMBER, BUT RETURN Nessun gioco
【问题讨论】:
-
看起来
sgameh有两个作用域。因此,这两个 console.log 语句都基于变量的两个不同范围返回值。 -
所以即使有请求,它也能像在答案中一样工作?
标签: javascript json node.js request