2017-10-05 55 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}); 
});