0

I'm writing a Node js program that will create a value by making an API call. and the program is as below.

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);

        }
    });

}

Here I want to assign the result to z and when I run my above program, z is printing nothing. but, x1.result.name is printing the exact value.

This is very confusing, can someone please let me know where am I going wrong and how can I fix this.

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • A small mistake is in these lines: `var x1 = JSON.parse(data); if (data) { console.log(x1.result.name); z += x1.result.name }`. Check if is `data` before declare `var x1 ...` like: `if (data) { var x1 = JSON.parse(data); console.log(x1.result.name); z += x1.result.name }` – Leonard Lepadatu Nov 21 '17 at 16:13
  • @LeonardLepadatu, no change :-( – user3872094 Nov 21 '17 at 16:16
  • Your `console.log("From inner " + z);` call is firing before the callback is fired because getTheDetailDetails makes an async call returning control to the outer loop. – Forty3 Nov 21 '17 at 16:21
  • @Forty3, yes exactly, I'm confused how I can fix this :( – user3872094 Nov 21 '17 at 16:22
  • @user3872094 - can you use the promise library? [Node.js Promises](https://www.npmjs.com/package/promise) – Forty3 Nov 21 '17 at 16:24
  • @Andy - You aren't going to close the question, are you? While the underlying issue may be an AJAX understanding issue, trying to glean an answer from the amazing detail in the referenced answers for the OP's immediate problem would be daunting. – Forty3 Nov 21 '17 at 16:30
  • try to `z += getTheDealerDetails(x.results[i].place_id, function (data) {if (data) {return JSON.parse(data); }})` – Leonard Lepadatu Nov 21 '17 at 16:31
  • @user3872094 - Actually, the first answer in the linked question which Andy considers this a duplicate has a great write up re: Promises. I would go that route. Best of luck! – Forty3 Nov 21 '17 at 16:35
  • @LeonardLepadatu, your solution is returning undefined now :( – user3872094 Nov 21 '17 at 16:39
  • obviously! sorry, now I saw that your callback is async. you should refactory your code – Leonard Lepadatu Nov 21 '17 at 16:41
  • @LeonardLepadatu, can you please help me with it? I'm not that good with Callbacks, The code that I'm using at my end is even an online searched one :( – user3872094 Nov 21 '17 at 16:44
  • Sorry! I don't have time to refactor your code but I create a fiddle for you with a small example: https://repl.it/repls/MaroonColossalKusimanse – Leonard Lepadatu Nov 21 '17 at 17:28

0 Answers0