Javascript 异步瀑布函数中的SailsJS嵌套回调未返回正确的值

Javascript 异步瀑布函数中的SailsJS嵌套回调未返回正确的值,javascript,asynchronous,callback,sails.js,waterfall,Javascript,Asynchronous,Callback,Sails.js,Waterfall,我正在用SailsJS创建一个应用程序。我有一个关于在异步瀑布和异步瀑布中使用回调的问题 在异步瀑布函数中有两个函数。第一个回调返回正确的值。第二个函数具有嵌套回调。但是,嵌套回调的值被固定在WaterlineFind函数中,并且永远不会返回到外部函数 您知道如何将值返回到外部函数吗 谢谢你的帮助 myFunction: function (req,res) { async.waterfall([ function(callback){ // native MongoDB

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

在异步瀑布函数中有两个函数。第一个回调返回正确的值。第二个函数具有嵌套回调。但是,嵌套回调的值被固定在WaterlineFind函数中,并且永远不会返回到外部函数

您知道如何将值返回到外部函数吗

谢谢你的帮助

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

使用async的函数时,应该在完成要执行的操作后使用回调。 例如:

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
         */
        }
    });
所以根据我的说法,你的代码应该是

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

应用此功能,您可以在最后的回拨中看到所需的内容。

您好,谢谢。现在,该值已传递,但未定义。你知道为什么吗?你得给我看看密码!我像我解释的那样使用异步,每个人都是这样做的。请检查并调试您的代码,否则请发送消息给我。谢谢,我非常感谢。代码在这里,