Home > Mobile >  How to Display Value Created from API Onto My Browser?
How to Display Value Created from API Onto My Browser?

Time:05-31

So I am working with this API and it auto calculates the delivery fee based on the address you input.

Here's the API docs I am using enter image description here

My issue is how do I get this value from the data field?

I tried to do

 const response = await client.createDelivery(
      {
        order_value: req.body.item1,
        fee: fee,
        tip: req.body.item1,
      },
      console.log(fee)
    );

    console.log(response);

    res.send(response);
  }
 )

But it says fee is not defined?

I also tried fee: "" and that doesn't work either.

I even put console.log(data.fee) and it says data is not defined

My last attempt I change it to console.log(response.fee) and it still showed undefined in the console?

How do I even get the fee value to console.log?

Note I am using express and for my tip value I have my input form named "item1" so I can access it by saying req.body.item1 to get that value

However, for the fee value its auto generated by the API, so I can't change it or update it manually myself.

CodePudding user response:

this is what the return object looks like

{
  data {
    currency: 'USD',
    fee: 975,
    otherData: otherData,
  }
}

what you must do to fix your problem is first dive into the data object then retrieve fee from that object like data.fee

const response = await client.createDelivery(
  {
    order_value: req.body.item1,
    fee: data.fee, // changed from fee to data.fee
    tip: req.body.item1,
  },
  console.log(data.fee)
);

without retrieving it from the data object there would be no fee object to grab which is whats making it undefined...

CodePudding user response:

Try using

console.log(response.data.fee)

And I am not sure what your client.createDelivery does. If it sends response, then you need to display it like

const response = await client.createDelivery(
  {
    order_value: req.body.item1,
    fee: fee,
    tip: req.body.item1,
  },
  console.log(fee)
).then((res) => res.json()).then((resData) => console.log(resData.data.fee));
  • Related