0

I have searched extensively through many answers, most of which are NOT directly related to retrieving the ZOOM data through AXIOS, but to no avail!

I want to call the ZOOM API via axios synchronously i.e. await for zoom api response before continuing, I have the following main (test-v02b.js) and my exported modules in zoom-v2b.js

I run node test-v02b.js It successfully returns my data

JWT_TOKEN; xxxxxxxxxxxxxxxxxxx
OPTIONS;{
  "Authorization": "Bearer xxxxxxxxxxxxxxxxxxxx",
  "User-Agent": "Zoom-api-Jwt-Request",
  "content-type": "application/json"
}
Data[object Promise]
Fetch Completed
Success; 2022-01-06T15:04:43Z

But as you can see the data has been returned after "supposed completion". Here is main test-v02b.js

const zoom = require('./zoom-v2b');
const token = zoom.jwtRefresh();

const axios = require('axios');

const options = {
  method: 'get',
  headers: {
    'Authorization': 'Bearer '+token,
    'User-Agent': 'Zoom-api-Jwt-Request',
    'content-type': 'application/json'
  }
};

// edit
(async function() { // added line
  console.log(`OPTIONS;${JSON.stringify(options.headers, null, 2)}`);

  const data = await zoom.zAXIOSv2(options); // added await
  console.log('Data'+data);

  console.log("Fetch Completed");
})(); // added line

Here is my file of exported modules zoom-v2b.js

// import for jwtRefresh
const jwt = require('jsonwebtoken');
const config = require('./config');

// import for zAXIOS
const axios = require('axios');

// Use the ApiKey and APISecret from config.js
function jwtRefresh() {
    const payload = { iss: config.APIKey, exp: ((new Date()).getTime() + 5000) };
    const token = jwt.sign(payload, config.APISecret);
    
    console.log(`JWT_TOKEN; ${token}`);

    return token;
}

module.exports = {
  jwtRefresh,
  zAXIOSv2: async (options) => {
    let response;
    const uriStr = 'https://api.zoom.us/v2/users/' + config.OWNER ;
    options.url = uriStr;

    try {
      response = await axios(options);
      console.log('Success;', response.data.user_created_at);
    }
    catch (err) {
      console.log('Error;', response.status);
    }
    return response.data ? response.data : response.status;
  }
};

As I said the call works but doesn't wait for a response, so I included the async / await in the exported module to try and cure this as stated by the posts I've read! Any help would be gratefully received? C. ** update ** Based on Edward's reply, I have now wrapped the top level statements like a psuedo main and now the await works as predicted.

chris-j
  • 61
  • 7

1 Answers1

0

As you can see in log, data you get back is [object Promise]. That is because you do not await async function. Add await keyword like this here:

const data = await zoom.zAXIOSv2(options);

Edited test-v02b.js to wrap all top level statements, with

(async function() {
// exisiting code
})();

as suggested in @Edwards comments

chris-j
  • 61
  • 7
Edward
  • 189
  • 2
  • 7
  • Including the above change, produces the following error; ```SyntaxError: await is only valid in async functions and the top level bodies of modules```; hence why the async function and await cmd are defined in the module (hope that makes sense) – chris-j Apr 14 '23 at 18:11
  • @chris-j Same way you put async in front of function, unless using latest NodeJS version, you need to put await in async function. So just wrap that code in async function and call it right away – Edward Apr 14 '23 at 19:04
  • I've tried so many combinations ... I thought the following, `zAXIOSv2: async (options) => {...}`, defined in modules.export relates to what you are saying. Could you be more explicit and provide a skeleton of the two files? Many thanks in advance for taking the time... – chris-j Apr 14 '23 at 20:15
  • Putting async there is correct, since you use await inside that function. Same way, in your file v02b.js you call await to await the data returned. Thus this part of code must be wrapped with async function. That's what error tells you. You use await without async function wrappers. (async function() { const data = zoom.zAXIOSv2(options); console.log('Data'+data); }()); – Edward Apr 14 '23 at 20:59
  • I wrapped the call to zoom.zAXIOSv2 and the subsequent print of data, like so; `(async function() { const data = await zoom.zAXIOSv2(options); console.log('Data', data); }());` with the await, the print of Data follows the print of fetch completed and success, wrong order! without the await, the print of data is pending the print of success still follows the fetch completed, wrong order! Hopefully, I've implemented it correctly? – chris-j Apr 14 '23 at 23:31
  • put fetch completed inside that self invoking function and it will be printed after data was loaded – Edward Apr 14 '23 at 23:47