0

Please see my code, i am not being able to find what's wrong in the code.

async makeSale({request,response,auth,params})
{
 const data=request.body.data
 var total=0
 _.forEach(data,(v)=>{
    total+=(v.productQuantity*v.productPrice)

})
  const saleData={seller_id:1, buyer_id:params.id,totalAmount:total}
  const [sale,config] = await Promise.all(this.createSale(saleData),this.getsConfig())
} 

and these are the two methods

createSale(s)
{
  console.log('One: '+new Date().getTime())
  const d=Sale.create(s)
  console.log(d) // this echo promise pending 
  return d
}
getsConfig()
{
  console.log('two: '+new Date().getTime())
  const c=Config.all()
  console.log(c) // this echo promise pending 
  return c
}

and the result in the console is

One: 1521967277914
Promise { <pending> }
two: 1521967277916
Promise { <pending> }

and the error is "undefined is not a function", name: "TypeError", status: 500

Thank you for your time.

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95

2 Answers2

2

I think the problem is that this.createSale(saleData).interate is not a function.

According to MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)

Promise.all accepts "iterable" as argument, and iterable can be an Array or String.

Your result :

One: 1521967277914
Promise { <pending> }

showed d of function:createSale() is a Promise but interable(String or array).

createSale(s)
{
  console.log('One: '+new Date().getTime())
  const d=Sale.create(s)
  console.log(d) // this echo promise pending 
  return d
}

So, maybe you can try code as follow:

await Promise.all([this.createSale(saleData),this.getsConfig()])
Guest
  • 111
  • 3
  • haha, thank you so much. I knew this but never saw I missed the array. Promise.all takes array my other functions also were working but this didn't simply because I missed []. Thanks once again :) – Hkm Sadek Mar 25 '18 at 09:51
  • @sadek Glad to help you – Guest Mar 25 '18 at 09:54
-1

this is because Promise.all are waiting for the createSale and getsConfig to be resolved and they are not promises so simply create a promise for them like below

createSale(s)
{

  return new Promise(function(resolve, reject) {
     console.log('One: '+new Date().getTime())
     const d=Sale.create(s)
     console.log(d) // this echo promise pending 
     resolve(d)
  });

}

and do the same thing for getsConfig

Fadi Abo Msalam
  • 6,739
  • 2
  • 20
  • 25
  • 1
    They are Promises. `Sale.create` and `Config.all` returns Promises, and the author does return the promises returned by those calls. – Sven Mar 25 '18 at 09:31
  • @Svenskunganka i didn't know that but from the console you can tell is that the promise is pending and that is the issue Promise { } – Fadi Abo Msalam Mar 25 '18 at 09:34
  • That's because the author is logging them directly after they are created with `console.log(d)` and `console.log(c)`. – Sven Mar 25 '18 at 09:35
  • @Svenskunganka but i belive that the console.log() shows the changed value of a variable and doesn't necessarily shows the value at that time reference - https://stackoverflow.com/questions/11284663/console-log-shows-the-changed-value-of-a-variable-before-the-value-actually-ch – Fadi Abo Msalam Mar 25 '18 at 09:43
  • Well, the variable `d` is never changed and explicitly assigned to the return value of `Sale.create` (which is a Promise). If `createSale` were an `async` function and the code was instead `const d = await Sale.create(s)`, the value of `d` would be the resolved value of the Promise rather than a Promise in pending state. – Sven Mar 25 '18 at 09:47