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.
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