SailsJS嵌套回调异步瀑布功能没有返回正确的值 [英] SailsJS Nested callback in async waterfall function not returning correct value

查看:407
本文介绍了SailsJS嵌套回调异步瀑布功能没有返回正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建与SailsJS的应用程序。我有一个关于使用异步瀑布和异步每个内回调的问题。

我有一个异步的瀑布函数内部两种功能。第一个回调函数返回正确的值。第二个函数有一个嵌套的回调。然而,嵌套回调的值被卡住的水线查找功能内,并且永远不会被返回到外部的功能。

你知道我怎么能值返回给外部函数?

感谢您的帮助。

  myFunction的:功能(REQ,RES){
async.waterfall([
    函数(回调){
        返回的对象数组// MongoDB的本地搜索功能
    },
    功能(结果,回调){
        async.each(resultAll,功能(位置){
            Model.find({位置:56d1653a34de610115d48aea'})。填充(位置)
            .exec(函数(ERR,项目,CB){
                的console.log(项目)//返回正确的值,但该值不能被传递给外部函数
            })
        })
        回调(NULL,结果);
    }],功能(错了,结果){
        //结果'D'
        res.send(结果)
    }
)}


解决方案

当您使用异步的功能,你应该,你应该使用你的回调,一旦你与你想执行操作完成。
例如:

  async.waterfall([
    功能(CB1){
        model1.find()。EXEC(函数(ERR,rows1){
            如果(!ERR){
                model2.find({})。EXEC(函数(ERR,rows2){
                    VAR ARR = [rows1,rows2]
                    CB1(NULL,ARR);
                });
            }其他
                CB1(ERR);
        })
    },功能(ARR,CB2){
    / **
     *这里的数组中....我们会发现rows1的阵列和rows2即[rows1,rows2]
     * * /
        model3.find()。EXEC(ERR,rows3){
            如果(!ERR){
                arr.push(rows3);
            //这个ARR现在将[rows1,rows2,rows3]
            }
            其他
                CB2(ERR);
        }
        CB1(NULL,ARR);
    }],
    功能(ERR,finalArray){
        如果(ERR){
        //错误处理
        }
        其他{
        / **
         *在finalArray我们有[rows1,rows2],rows3是不存在的最后一个数组
         *东阳CB2()与异步model3.find回呼()。第二功能的exec()
         * /
        }
    });

所以根据我你的code应该是这样

  async.waterfall([
        函数(回调){
            返回的对象数组// MongoDB的本地搜索功能
        },
        功能(结果,回调){
            async.each(resultAll,功能(位置){
                Model.find({位置:56d1653a34de610115d48aea'})。填充(位置)
                    .exec(函数(ERR,项目,CB){
                        的console.log(项目)//返回正确的值,但该值不能被传递给外部函数
                        回调(NULL,yourData);
                        / **
                         * yourData是任何你想要的最终回调的第二个参数来收到的数据
                         *即回调(NULL,项目)或回调(NULL,[结果,项目])或回调(NULL,结果)
                        * /
                    })
            })
        }],功能(错了,结果){
        //结果'D'
        res.send(结果)
    }
);

这个应用,你可以看到在最终的回调你想要的东西。

I'm creating an application with SailsJS. I have a question about using callbacks within an Async waterfall and Async each.

I've got two functions inside an async waterfall function. The first callback returns the correct value. The second function has a nested callback. However, the value of the nested callback is stuck within the waterline find function, and never gets returned to the outer function.

Do you know how I could return the value to the outer function?

Thanks for your help.

myFunction: function (req,res) {
async.waterfall([
    function(callback){
        // native MongoDB search function that returns an Array of Objects
    },
    function(result, callback){
        async.each(resultAll, function (location){
            Model.find({location:'56d1653a34de610115d48aea'}).populate('location')
            .exec(function(err, item, cb){
                console.log (item) // returns the correct value, but the value cant be passed to the outer function
            })
        })
        callback(null, result);
    }], function (err, result) {
        // result is 'd'
        res.send (result)
    }
)}

解决方案

When you use async's function you should you should use you callback once you are done with operations you want to perform. for example:

async.waterfall([
    function(cb1){
        model1.find().exec(function(err,rows1){
            if(!err){
                model2.find({}).exec(function(err,rows2){
                    var arr=[rows1,rows2];
                    cb1(null,arr);
                });
            }else
                cb1(err);
        })
    },function(arr,cb2){
    /**
     * here in the array....we will find the array of rows1 and rows2 i.e.[rows1,rows2]
     * */
        model3.find().exec(err,rows3){
            if(!err){
                arr.push(rows3);
            //  this arr will now be [rows1,rows2,rows3]
            }
            else
                cb2(err);
        }
        cb1(null,arr);
    }],
    function(err,finalArray){
        if(err){
        //  error handling
        }
        else{
        /**
         * in finalArray we have [rows1,rows2] ,rows3 is not there in final array
         * beacause cb2() was callbacked asynchronously with model3.find().exec() in second function
         */
        }
    });

so according to me your code should be like

async.waterfall([
        function(callback){
            // native MongoDB search function that returns an Array of Objects
        },
        function(result, callback){
            async.each(resultAll, function (location){
                Model.find({location:'56d1653a34de610115d48aea'}).populate('location')
                    .exec(function(err, item, cb){
                        console.log (item) // returns the correct value, but the value cant be passed to the outer function
                        callback(null, yourData);
                        /**
                         * yourData is the data whatever you want to recieve in final callbacks's second parameter
                         * i.e. callback(null,item) or callback(null,[result,item]) or callback(null,result)
                        */
                    })
            })
        }], function (err, result) {
        // result is 'd'
        res.send (result)
    }
);

Apply this and you can see things you want in your final callback.

这篇关于SailsJS嵌套回调异步瀑布功能没有返回正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆