【问题标题】:Node.js - Return Array from functionNode.js - 从函数返回数组
【发布时间】: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


【解决方案1】:

您无法从回调中返回

  router.get('/:ID', function(req, res, next) {
    var myRenderArray = [];

    //use callback instead 
    auxFunctions.myArrayFunc(req.params.ID ,function(myRenderArray){

        if(myRenderArray){
         res.render('myView', {title: 'title', myRenderArray});
     }
 });

});


module.exports.myArrayFunc = function myArrayFunc(ID ,callback){
    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 using callback
                return callback(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); });
}

【讨论】:

  • 还是不行。我放了一些console.log,好像函数没有执行。
  • 尝试仅在定义的地方将 module.exports.myArrayFunc 更改为 exports.myArrayFunc
【解决方案2】:

此处代码的唯一相关部分正是您的问题中未包含的内容 - 不返回数组的函数的源代码。唯一的其他相关数据是错误消息。在没有看到该代码或错误消息的情况下,我只能猜测,但您可能在那里做的事情很少:您可能正在为数组使用全局变量或外部范围中的变量,并且它在调用之间共享。您可能正在使用res 或其他一些在请求处理程序中可用但在该函数中不可用的变量。这都是猜测,因为您没有包含函数的源代码或错误消息,因此无法提供更详细的信息。

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 2020-09-12
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多