0

I am new to node.js and I have written code using async.forEach().Something is wrong I am not getting my mistake.

async.forEach(payload.bookings,fun,function(err,res){
 var data ;
 if(err)
 console.log(err);
 if(res) {
 data = res;
 console.log(data);
 }
 });
 function fun(booking,cb){
 var dataToSave = {};
 //service choosen by customer
 var service_id = booking.serviceId;
 dataToSave.serviceId = service_id;
 var query = {
 services : {$elemMatch : { serviceId : service_id } }
 };
 var projection = {
 cellPhoneNumber :1,
 countryCode:1,
 services :1
 };
 var options = {};
 Service.serviceProvider.getServiceProviders(query,projection,options,function(err,data){
 if(err)
 cb(err);
 else{
 if(data && data.length>0){
 //getting list of SP which gives the service name given by customer
 dataToSave.serviceProviderList = [];
 for(var sp=0;sp<data.length;sp++){
 var serviceProvider = {};
 serviceProvider.id = data[sp]._id;
 //searching in services array of one particular service id
 for(var j=0;j<data[sp].services.length;j++){
 if(data[sp].services[j].serviceId==service_id)
 {
 serviceProvider.serviceRate = data[sp].services[j].rate;
 break;
 }
 }
 dataToSave.serviceProviderList.push(serviceProvider);
 }
 cb(dataToSave);
 }
 else{
 cb();
 }
 }
 });
 };

Output:

{ serviceId: '578e0da7c8c4cfc807040852',
 serviceProviderList: 
 [ { id: 578e18f6ed564791083c75ad, serviceRate: 200 },
 { id: 578e1996ed564791083c75b4, serviceRate: 200 } ] }

Expected output :

{ serviceId: '578e0da7c8c4cfc807040852',
 serviceProviderList: 
 [ { id: 578e18f6ed564791083c75ad, serviceRate: 200 },
 { id: 578e1996ed564791083c75b4, serviceRate: 200 } ] }
{ serviceId: '578dca50e93da81f0423f6b6',
 serviceProviderList: [ { id: 578e18f6ed564791083c75ad, serviceRate: 100 } ] }

I have seen on swagger documentation.When I click on Try It Out it keeps on running.only it shows the object which I have printed using console.log()

asked Jul 20, 2016 at 6:29
5
  • Which version of async are u using? Commented Jul 20, 2016 at 7:08
  • @Iceman "async": "^2.0.0-rc.6" Commented Jul 20, 2016 at 7:11
  • Couldn't find .forEach() in the docs, though you write it exactly with .each() syntax. Commented Jul 20, 2016 at 7:13
  • payload.bookings is array or object Commented Jul 20, 2016 at 7:49
  • @FooVirus payload.bookings is an array of objects Commented Jul 20, 2016 at 8:25

1 Answer 1

1

The problem probably lies in the cb(dataToSave); line. The pattern for the callbacks is cb(err, results);. By executing cb(dataToSave); the async.js believes you ran into an error.

Try changing it to cb(null, dataToSave);

 (...)
 }
 dataToSave.serviceProviderList.push(serviceProvider);
 }
 cb(null, dataToSave);
 }
 (...)
answered Oct 19, 2016 at 22:23
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.