1

I have 2 methods and want to call one method in the other method. but I get an exception

(node:17364) UnhandledPromiseRejectionWarning: ReferenceError: GetVouchers is not defined

class  VoucherRepository
{
  AddNewVoucher(newVoucher) {
       return new Promise(function(resolve, reject) {
       //  ...
        DBModel.Voucher.create(newVoucher).then((result)=>{ // when it completed

            (async function(){
            let vouchers =await GetVouchers();
            resolve(vouchers);
          })();
        });
    });
  }    

  async GetVouchers()
  {        
    console.log('getVouchers');    
    return new Promise(function(resolve, reject) {
    DBModel.Voucher.find({}, function(err, result) {
        if (err) throw err;

      var voucherList=[];
     // .....      
      resolve(voucherList);

    });        
    });
  }
}    
module.exports = VoucherRepository;

as can bee seen, I call GetVouchers() method in AddNewVoucher method after I've created a voucher. I believe that it is some how related with Promise but I could not solve.

and I call AddNewVoucher as follow

var voucherRepository = new VoucherRepository();
return voucherRepository.AddNewVoucher(newVoucher);

PS: I can call GetVouchers alone and it works..

return voucherRepository.GetVouchers();

Update: I've already tried this keyword but I get the same error.

let vouchers =await this.GetVouchers();//router.getVouchers();

(node:16364) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'GetVouchers' of undefined

ertan2002
  • 1,012
  • 1
  • 22
  • 60

0 Answers0