【发布时间】:2017-08-20 08:47:12
【问题描述】:
我有一个函数,它返回一个用 myArray.push({"key":value}) 方法创建的数组
在路由器文件中我做了这个,但是没有用:
router.get('/:ID', function(req, res, next) {
var myRenderArray = [];
myRenderArray = auxFunctions.myArrayFunc(req.params.ID);
res.render('myView', {title: 'title', myRenderArray});
});
如果我将整个函数放在路由器文件中,并更改res.render('myView', {title: 'title', myRenderArray}); 的返回语句,它就可以工作!
恢复功能代码:
module.exports.myArrayFunc = function myArrayFunc(ID){
var myArray = [];
var id = req.params.ID;
var req1 = new dbConfig1.Request();
var req2 = new dbConfig2.Request();
req1.query(query1('foo', ID))
.then(function (array1) {
req2.query(query2('foo', id, array1[0].fooId))
.then(function (array2) {
req1.query(query3(array1[0].fooId))
.then(function (array3) {
req1.query(query4('foo', ID, array1[0].fooId))
.then(function (array4) {
myArray.push({
'name1': key1,
'name2': key2,
'name3': key3,
'name4': key4,
});
return myArray;
})
.catch(function (err) { console.log('****** Error on query 4'); console.log(err); });
})
.catch(function (err) { console.log('****** Error on query 3'); console.log(err); });
})
.catch(function (err) { console.log('****** Error on query 2'); console.log(err); });
})
.catch(function (err) { console.log('****** Error on query 1'); console.log(err); });
}
我该怎么办?
谢谢!
【问题讨论】:
-
你能试试这个 res.render('myView', {title: 'title', array: myRenderArray});
标签: arrays node.js function express