2017-10-05 53 views
0

我正在对我的json进行一些筛选并将其保存在驱动程序变量中。我的驱动程序变量正在返回JSON数据,我想将它们原样发送到视图而没有任何更改。将JSON数据发送到NodeJs中的视图

我的问题是:如何将驱动var发送给视图?

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

回答

1

移动res.renderrp.then并传递driver它作为res.render参数另一属性。

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

}); 

请仔细阅读这也How do I return the response from an asynchronous call?

编辑:

你也应该在catch处理程序使用next

.catch(function (err) { 
    next(err); 
}); 
+0

非常感谢你:* – MrBlue

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