SailsJS异步瀑布函数中的嵌套回调未返回正确的值(SailsJS Nested callback in async waterfall function not returning correct

编程入门 行业动态 更新时间:2024-10-28 08:24:02
SailsJS异步瀑布函数中的嵌套回调未返回正确的值(SailsJS Nested callback in async waterfall function not returning correct value)

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

我在异步瀑布函数中有两个函数。 第一个回调返回正确的值。 第二个函数有一个嵌套的回调函数。 但是,嵌套回调的值卡在水线查找功能中,永远不会返回到外部函数。

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

谢谢你的帮助。

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

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

最满意答案

当您使用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) } );

应用此选项,您可以在最终回调中看到所需的内容。

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.

更多推荐

本文发布于:2023-07-31 09:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   回调   瀑布   函数   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!