混淆在forloop中附加值

时间:2017-11-21 16:09:39

标签: javascript node.js

我正在编写一个Node js程序,它将通过进行API调用来创建一个值。程序如下。

request(postData, function (error, resp, body) {
        console.log("Status code " + resp.statusCode);
        if (!error && (resp.statusCode == 200 || resp.statusCode == 201)) {
            var z = "";

            if (body) {
                var x = JSON.parse(body);
                for (var i = 0; i <= 5; i++) {
                    getTheDealerDetails(x.results[i].place_id, function (data) {
                        var x1 = JSON.parse(data);
                        if (data) {
                            console.log(x1.result.name);

                            z += x1.result.name
                        }
                    });
                }
                console.log("From inner " + z);

            } else {
                console.log("I am unable to authenticate you. please disable the skill and re link your account");
                callback("I am unable to authenticate you. please disable the skill and re link your account");
            }
        } else {
            console.log(error);
            callback(error);

        }
    });



function getTheDealerDetails(paceId, callback) {
    var postData = {
        uri: "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + paceId + "&key=myKey",
        method: "GET",
    };


    request(postData, function (error, resp, body) {
        if (!error && (resp.statusCode == 200 || resp.statusCode == 201)) {
            if (body) {
                var x1 = JSON.parse(body);
                callback(body);
            } else {
                console.log("I am unable to authenticate you. please disable the skill and re link your account");
                callback("I am unable to authenticate you. please disable the skill and re link your account");
            }
        } else {
            console.log(error);
            callback(error);

        }
    });

}

这里我想将结果分配给z,当我运行上面的程序时,z什么都不打印。但是,x1.result.name正在打印确切的值。

这非常令人困惑,有人可以让我知道我哪里出错了,我该如何解决这个问题。

由于

0 个答案:

没有答案