I have a request made with "request" from NodeJs, and I want to get the data with a promise. All good because the console shows me the information that I am requested. The problem is when I call the promise function, and the console shows me this. How can I get the real value?
let leagueTier = (accId) => new Promise(async (resolve, reject) => {
request({
url: `https://${reg}.api.riotgames.com/lol/league/v4/entries/by-summoner/${accId}${key}`,
json: true
}, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(body[0].tier.toLowerCase())
} else {
reject(Error('It broke'))
}
})
}).then(function(res) {
return res;
})
This is where I call the function
.attachFiles([`./images/${leagueTier(body.id)}.png`])
(node:5868) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'D:\Dev\shen-bot\images[object Promise].png'
How can I access the string of the request I made? Maybe I shouldn't use promises?
The leagueTier
return value will be a promise that eventually resolves or rejects into a value. You can't simply add the return value as part of a string. You can either use await
or then
to wait for the value to come through.
// asuming you are in async function context
.attachFiles([`./images/${await leagueTier(body.id)}.png`]);
// or
leagueTier(body.id).then(tier => {
.attachFiles([`./images/${tier}.png`]);
});
Note that your current code:
.then(function(res) {
return res;
})
Does absolutely nothing and can be omitted.