Home > Back-end >  Cant assign response to a result object
Cant assign response to a result object

Time:12-13

I have an api from Twilo that I am trying to use to get some data from there API. I can print the result to console but for some reason i cant assign the phone_number response to the result and return it.

then(phone_number => result = phone_number)

What am i missing here ?

apiRoutes.get('/callerid/:number', async (req, res) => {
  let result
  client.lookups.v1.phoneNumbers(req.params.number)
      .fetch({type: ['carrier', 'caller-name']})
      .then(phone_number => console.log(phone_number)
      );

  console.log(result)
  res.json({ Success: true , Data: result })

})

CodePudding user response:

try to save the whole promise in the result variable

you must return phone_number in the last then

apiRoutes.get('/callerid/:number', async (req, res) => {

  let result = await client.lookups.v1.phoneNumbers(req.params.number)
      .fetch({type: ['carrier', 'caller-name']})
      .then(phone_number => phone_number);

  console.log(result)
  res.json({ Success: true , Data: result })

})
  • Related