0

i'm doing some filtering on my json and saving it in a driver variable. I the driver variable is returning JSON data and i want to send them as they are to the view without changes.

my question is: how can i send the driver var to the view ?

router.get('/', function(req, res, next) {
rp(options)
    .then(function (repos) {
        var obj = repos;
        var driver = _.filter( obj.features, ['id', 'tmp_location.20658']);
        console.log(driver);        })
    .catch(function (err) {
        // Delete failed...
    });
   res.render('index', { title: 'Express' });
});
Community
  • 1
  • 1
MrBlue
  • 3
  • 3

2 Answers2

1

Move res.render to rp.then and pass driver it as another property in res.render parameters.

router.get('/', function(req, res, next) {
rp(options)
    .then(function (repos) {
        var obj = repos;
        var driver = _.filter( obj.features, ['id', 'tmp_location.20658']);
        console.log(driver);
        res.render('index', { title: 'Express', driver: driver });
    })
    .catch(function (err) {
        // Delete failed...
    });

});

Please read this also How do I return the response from an asynchronous call?

Edit:

You should also use next in catch handler

.catch(function (err) {
    next(err);
});
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
0
router.get('/', function(req, res, next) {
rp(options)
.then(function (repos) {
    var obj = repos;
    var driver = _.filter( obj.features, ['id', 'tmp_location.20658']);
    console.log(driver);        })
.catch(function (err) {
    // Delete failed...
});
res.render('index', { title: 'Express' , data:driver});
});
swapnil2993
  • 1,384
  • 11
  • 15