angular - Promise - Getting read as promise in one place and as 'undefined' in another -


i'm creating new promise (only method in class returning promise)

return new promise(function(resolve, reject) {      ... mongo db call     if ( err ) {reject(err)}     if ( data ) {resolve(data)} }) 

when return this, in 1 place i'm able read promise , data using

promise.then(..) 

but, when try doing same thing in class (it in first line in both classes), i'm getting error reads,

[typeerror: cannot read property 'then' of undefined] 

has faced similar issue?

edit: suggested, actual code -

module.exports = {   populatedashboard: function ( req, callback ) {      var userinfo = {username: 'business user1', role: 'business'};     console.log ('userinfo - ', userinfo); // getting value, obviously!     var promise1 = roleaccessrepository.getuserroleaccess (userinfo.role);     promise1.then (         function(data) {          }     ).catch(         function(err) {             console.log('error', err); // error getting caught here         }     )} }    module.exports = {     fetchcampaigninfo: this.fetchcampaigninfo, }     fetchcampaigninfo = function ( role, username, sectionname, callback ) {        var campaignlist = [];       var promisearray = [];        var promise1 = roleaccessrepository.getuserroleaccess (role);       promise1.then (         onrolesuccess = function ( data ) {             // goes through fine         }     ).catch(         function(err) {             console.log('error', err);          }     )} }  module.exports = {   getuserroleaccess: function ( role ) {     return new promise (       function ( resolve, reject ) {         var roleaccess = mongoose.model ('roleaccess', useraccessrole);         roleaccess.find ({role: role}, function ( err, data ) {           if ( err ) {             console.log ('getuserroleaccess - error', err);             reject (err);           }           console.log ('getuserroleaccess - data', json.stringify(data));           resolve (data);         });        });   } } 

i'll make answer since apparently led figuring out problem:

it hit .catch() if code inside promise1.then() handler right before threw exception. don't show of code have no idea whether happening, that's way hit .catch().

all .then() handlers called "throw-safe". means if code in .then() handler throws exception, automatically caught promise's .then() handling code , promise rejected exception reason rejection. so, chained handlers after .then() handler threw exception see rejected promise. means promise returned promise1.then() becomes rejected promise1.then().catch() hit .catch() handler.

here's simple example illustrate:

function delay(t) {      return new promise(function(resolve) {          settimeout(resolve, t);      });  }    delay(100).then(function() {      console.log("#1");      throw new error("some error");  }).then(function() {      console.log("#2");  }).catch(function(err) {      console.log("#3 ", err.message);  });

this show output:

#1 #3  error 

Comments