javascript - AsyncNodeJS: Error: Callback was already called -


i born new node.js , mercilessly got solve bug in written coding. trying update group members following service , whenever try call api, got error as

error: callback called.

my code is:

'app.post('/api/updategroup/:group_id', function (req, res) {   var checkgroup = group.findone({'_id': req.params.group_id}).exec();     checkgroup.addback(function( err , existinggroup ) {         if ( err ) {             res.json({'message' : err });         }else if( existinggroup ){  group.findone({ '_id': req.params.group_id })         .execq()         .then(function(existinguser) {             var friendphonenumber = req.body.friendphonenumber.split(',');              var friends = [];             console.log('existinguser', friendphonenumber);              async.each(friendphonenumber, function(phonenum, callback) {                  var phonenum = phonenum.split("\'")[0];                 console.log('phonenum', phonenum);                  user.findone({                     'phonenumber': phonenum                 })                 .execq()                 .then(function(existingfriend) {                      if(existingfriend === null) {                         friends.push({                             'details': {                                 'phonenumber': phonenum                             }                         });                         callback();                      } else {                          friends.push({'details': existingfriend});                         callback();                     }                  })                 .catch(function(err) {                     console.log('err', err)                     friends.push({                         'details': {                             'phonenumber': phonenum                         }                     });                     callback();                  })                 .done(function() {                  });              }, function(err) {                 console.log('callback')                  group.group_id = req.params.group_id;                 // group.user_id = req.body.userid;                 // group.createddate = moment().format('yyyy-mm-dd hh:mm a');                  group.friends = friends;                  group.update(function(err) {                     if (err) {                         res.json({                             message: err                         });                     } else {                          res.json({                             success: 1,                             message: 'group updated',                             group: group                         });                     }                 });               });          })         .catch(function(err) {             res.json({                 success: 0,                 message: 'user id not match. please try again'             });          })         .done(function(events) {           });     }     else {          console.log('nope');     }  });   }); 

please me issue... have seen few answers regarding same, still cant solve it!

if callback throws error, going called in both then , following catch. should calling in done function.

            .then(function(existingfriend) {                  if(existingfriend === null) {                     friends.push({                         'details': {                             'phonenumber': phonenum                         }                     });                 } else {                      friends.push({'details': existingfriend});                 }              })             .catch(function(err) {                 console.log('err', err)                 friends.push({                     'details': {                         'phonenumber': phonenum                     }                 });              })             .done(function() {                 callback();             }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -