0

getAccomodationCost is a function which is expected to return a promise with a return value. Now It's throwing an error resolve is undefined.

This error message is thrown at line resolve(JSON.parse(JSON.stringify(result))) inside promise then. If i replace keyword resolve with return then Promise.all call in the main function will fail.

Can some one help me to return a promise with a return value JSON.parse(JSON.stringify(result)) from the below function.

  var getAccomodationCost = function (req, res) {

       var accomodationCostPromise = new Promise(function (resolve, reject) 
        {
        getHospitalStayDuration(req, res, function (duration) {
            resolve(duration)            
        })
     })
    .then(function (duration) {
        hotelModel.aggregate([
           //Some logic here
        ], function (err, result) {            
           resolve(JSON.parse(JSON.stringify(result)))          
        })

   })
   return accomodationCostPromise;
}

   //Main function where the above snippet is called   
    const promise1 = somefunction(req, res);
    const accomodationCostPromise = getAccomodationCost(req, res)   
    Promise.all([promise1,accomodationCostPromise])
    .then(([hospitalInfo,accomodationCost]) => {        
        //Return some json response from here
    }).catch(function (err) {
        return res.json({ "Message": err.message });
    });    
Sona Shetty
  • 997
  • 3
  • 18
  • 41
  • First, you probably must return hotelModel.aggregate() (and not just invoke it). Second, i don't know about your aggregate function, but from what I can see, there is no resolve method reachable in the function you pass in. Remember you always must return the result so that you can chain promises. – sjahan Oct 08 '17 at 18:07
  • You need to create a second `new Promise` to get a `resolve` for the `hotelModel.aggregate` callbck – Bergi Oct 08 '17 at 19:20

2 Answers2

2

If possible have hotelModel.aggregate return a promise. That'd make the code look something like this:

.then(function (duration) {
    return hotelModel.aggregate([
       //Some logic here
    ]).then(result => JSON.parse(JSON.stringify(result))) // Not sure why you're stringify/parsing
 })

If you cannot modify hotelModel.aggregate to return a promise, you will need to create another promise and return that from .then(function (duration), similar to how you did it for getHospitalStayDuration.

Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
-2

A Promise can only be fulfilled once. resolve() is called twice within function, resolve is not defined within .then(). resolve is defined within Promise constructor executor function. A second Promise should be used within .then().

var getAccomodationCost = function (req, res) {
  return new Promise(function (resolve, reject) {
        getHospitalStayDuration(req, res, function (duration) {
            resolve(duration)            
        })
     })
    .then(function (duration) {
       return new Promise(function(resolve, reject) {
         hotelModel.aggregate([
           //Some logic here
         ], function (err, result) {  
           if (err) reject(err);          
           resolve(JSON.parse(JSON.stringify(result)))          
         })
       })
     });
}
guest271314
  • 1
  • 15
  • 104
  • 177