1
var payUrl;
var orderId;

async function getInvoice() {
    var data = JSON.stringify({
        "price_amount": 10,
        "price_currency": "gbp",
      });
      
      var config = {
        method: 'post',
        url: 'https://api.nowpayments.io/v1/invoice',
        headers: { 
          'x-api-key': 'api-key', 
          'Content-Type': 'application/json'
        },
        data : data
      };
      
      axios(config)
      .then(function (response) {
        orderId = response.data.id;
        payUrl = response.data.invoice_url;
      });
}

getInvoice();

console.log(payUrl);

This code returns undefined. I need the code to set the global variable to the response data needed so i can access it in other functions.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • first of all why using async function and use .then? have you console.log(response) to see if it's working? – Hen Moshe Feb 15 '22 at 14:58
  • console.log(payUrl); returns the url inside the .then function. But when called outside the .then it returns undefined. – Haytch Accbae Feb 15 '22 at 14:59

0 Answers0