1

I'm using express with node for my rest api, I need to run a for loop to determine the output json. my route file looks

var Redis = require('ioredis')
var redis = new Redis({
    port: 6379,
    host: '127.0.0.1',
    family: 4,
    password: 'password',
    db: 0
});

var Jsonresult = {};
var process = function(lat,lon,dist,unit)
{
    Jsonresult.result = 'success';
    var vehicle_type = new Array('small','medium');  
    vehicle_type.forEach(function(vehicle, index, arr) 
    {
        redis.georadius ( vehicle,lat,lon ,dist,unit,'WITHCOORD','WITHDIST',function( ERR , Result ) 
        {
            if (ERR) 
            {
                console.log(ERR);
            }

            Jsonresult[vehicle] = Result;
        }) ;
    })

    return Jsonresult;
}

router.get('/:lat/:lon/:dist/:unit', function(req, res, next) {
    var lat = req.params.lat;
    var lon = req.params.lon; 
    var dist = req.params.dist;
    var unit = req.params.unit;
    res.json(process(lat,lon,dist,unit));
});

module.exports = router;

and my expected json output is

{"result":"success","small":[["driver_1","0.2779",["56.507199704647064","-0.12500104133338397"]],["driver_2","0.2782",["56.50730162858963","-0.12500104133338397"]]],"medium":[]}

but i'm getting only

{"result":"success"}

whats wrong in the code ?

6
  • did you pass Jsonresult in res.json(); ? Commented Feb 8, 2016 at 19:33
  • yes i added that variable Commented Feb 8, 2016 at 19:36
  • Why are you using require('async-foreach').forEach? Commented Feb 8, 2016 at 19:39
  • What do you pass when you pass a GET request? can you provide an example? Commented Feb 8, 2016 at 19:52
  • I have updated the question and my get parameters are /vehicles/56.5072/-0.1275/5/km Commented Feb 8, 2016 at 19:59

2 Answers 2

6

this way:

vehicle_type.forEach(function(vehicle, index, arr) {
   Jsonresult[vehicle] = true;
})

and pass result in response as well when calling res.json()..

res.json(Jsonresult);

Sign up to request clarification or add additional context in comments.

5 Comments

Hi i have tried this also. but when i refresh the page the expected result is coming. but first time not coming.
Maybe you can explain what you changed and why.
then, i guess, use res.render(Jsonresult) instead of res.json(Jsonresult);. let me know if this works.
@FelixKling : i added res.json(Jsonresult);
That's not the only thing you've changed as far as I can see.
0
vehicle_type.forEach(function(vehicle) 
{
    calls.push(function(callback){
        redis.georadius ( vehicle,lat,lon ,dist,unit,'WITHCOORD','WITHDIST',  function  ( ERR , Result ) 
        {
            if (ERR) 
            return callback(ERR);  

            Jsonresult[vehicle] = Result;
            callback(null, vehicle);


        });


      });
});

async.parallel(calls, function(err, result) {
if (err)
    return console.log(err);
res.json(Jsonresult);
 });

This finaly solve my problem: answer here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.