0

I have a problem access value outside callback, I have a json like below

[ { Latitude: '3.59288601404616',
    Longitude: '98.6827551573515',
    Latitude1: '3.5828173285855724',
    Longitude1: '98.69136337190866',
  },
   { Latitude: '3.65865664490563',
     Longitude: '98.6652326211333',
     Latitude1: '3.5828173285855724',
     Longitude1: '98.69136337190866',
  } ]

I have read a lot of web said that its impossible to get value from the callback because we have to wait until the callback finish first then we can have a value and some question in stackoverflow provide some solution but no one is work for me.

What I want to get is to add a new json inside that json that I post above, I want to add Distance json inside so I use node-google-distance

What I want is like this

[ { Latitude: '3.59288601404616',
    Longitude: '98.6827551573515',
    Latitude1: '3.5828173285855724',
    Longitude1: '98.69136337190866',
    Distance '10127'
  },
   { Latitude: '3.65865664490563',
     Longitude: '98.6652326211333',
     Latitude1: '3.5828173285855724',
     Longitude1: '98.69136337190866',
     Distance: '1836'
  } ]

and my code like this

models.xxxx.findAll({
    where: {
        UserId: req.params.UserId
    },
    attributes: ['Latitude', 'Longitude', [sequelize.literal('(SELECT Latitude FROM `xxxx` WHERE `id` = ' + req.params.MerchantId + ')'), 'Latitude1'],
        [sequelize.literal('(SELECT Longitude FROM `xxxx` WHERE `id` = ' + req.params.MerchantId + ')'), 'Longitude1']
    ]
}).then(function(lists) {

    var json = JSON.parse(JSON.stringify(lists));

    function get_count(item, callback) {
        var array = [];
        for (var i = 0; i < json.length; i++) {
            distance.get({
                    origin: json[i].Latitude1 + ',' + json[i].Longitude1,
                    destination: json[i].Latitude + ',' + json[i].Longitude
                },
                function(err, data) {
                    if (err) {
                        array.push(0);
                    } else {
                        array.push(data.distanceValue);
                    }

                    item.distanceValue = array;
                });
        }
        callback(null, item);
    }
    async.map(json, get_count, function(err, results) {
        console.log(results);
    });
});

I use async.map because in this web someone post it Javascript: return a value to a variable outside of a callback function, I spend a lot of time search in stackoverflow but found nothing

NodeJS Can't Access Variable Inside Callback

How to return value from an asynchronous callback function?

NodeJS get async return value (callback)

Site 1

Site 2

Please give me some hint or some example how to fix this problem

Community
  • 1
  • 1
Dirus
  • 1,033
  • 2
  • 14
  • 21
  • Several questions/issues: 1. Are `json` and `jsonData` meant to be the same variable? If not, where does `jsonData` come from? 2. Note that `json` refers to something that by definition **isn't** JSON (it's been parsed). 3. `JSON.parse(JSON.stringify(x))` is both an inefficient and incorrect way to clone an object tree. – T.J. Crowder Aug 05 '16 at 08:54
  • @T.J.Crowder `json` and `jsonData` actually same, I just have misplaced copy paste the content, and about `JSON.parse(JSON.stringify(x))` I don't have a problem with that because it still display the result, my problem is I can't add a new json into it – Dirus Aug 05 '16 at 08:58
  • 2
    Again: Once you parse it, it's not JSON. JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Aug 05 '16 at 09:00
  • @T.J.Crowder can you post some example about it ? actually I can't get any value from `var array = [];` when I use console.log the `get_count` give me empty array so I can't test it – Dirus Aug 05 '16 at 09:11
  • Simple: `var json = '{"foo":"bar"}'; var notJson = JSON.parse(json);` – T.J. Crowder Aug 05 '16 at 09:12

0 Answers0