Here's a function I'm writing to talk to an api using axios. I'm struggling with getting this function to return the an array. I understand that I get a promise out of this, but when I chain then() onto my function with the intent to use the data from the promise, it has nothing in it. I can put in a console.log(reqs) instead of the return value and it will print the data I'm interested in along with an 'undefined' line at the top, though I'm not quite sure why. I'm trying to avoid the use of console.log with the exception of testing my function where possible as I don't want this data viewable from the console.
Here is my function.
async function getMembers(omeIp,omeUser,omePass) {
var reqs = [];
axios({
method: 'get',
url: `https://${omeIp}/redfish/v1/Systems/Members`,
auth: {
username: omeUser,
password: omePass
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
headers: {'Self-Signed-Header': 'certificate'}
}).then((res) => {
value = res.data['value'];
for(var key in value) {
let member = value[key]['@odata.id'];
reqs.push(member);
}
return reqs;
}).catch((err) => {
console.log(err);
});
};
module.exports = {
getMembers,
};
and here is what I'm calling it with.
const {getMembers} = require('./functions.js');
var omeIp = "100.73.32.21",
omeUser = "admin",
omePass = "Sq%9A&pEBP";
getMembers(omeIp,omeUser,omePass).then((res) => console.log(res));
How do I get the result from my function to allow me to chain a then() onto the end and use the contents of the promise? or is there a better way to do this?