AxiosError: connect ECONNREFUSED 127.0.0.1:80

I have a nodejs application and I have a axios inside it, because I need to call a api to populate the response of my api, but when I deployed my application, I'm getting this error from axios, someone know what is it?
49 Replies
Percy
Percy2y ago
Project ID: N/A
Percy
Percy2y ago
⚠️ experimental feature
Brody
Brody2y ago
This could be a lot of things, could I see a repo please?
lucaszdevmmyers616
N/A It's private, but I can send the Axios call: const request = axios.create({ baseURL, timeout: 10000, // 10s timeout headers: { Accept: "application/json", "Content-Type": "application/json" }, }); I'm only getting request and using axios normally
Brody
Brody2y ago
What's the baseURL set to
lucaszdevmmyers616
the url of the api I want to call
Brody
Brody2y ago
and that is?
lucaszdevmmyers616
https://apiloterias.com.br/app export default class LotteryApi { getLottery = async (lottery) => { try { const { data } = await request.get('/resultado', { params: { loteria: lottery } }); return data; } catch (error) { console.error(error); } }; } this way I call the endpoint I want and return the data
Brody
Brody2y ago
it would significantly help if i could see the repo
lucaszdevmmyers616
Why??? it's only this I send you, there is nothing more it's only the call I only call: lotteryApi.getLottery(...)
Brody
Brody2y ago
its not much to go off of show me a screenshot of the error?
lucaszdevmmyers616
ok
Brody
Brody2y ago
likely wrong syntax
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
https://www.npmjs.com/package/axios#user-content-creating-an-instance
lucaszdevmmyers616
No, in localhost works fine
Brody
Brody2y ago
try with the correct syntax
lucaszdevmmyers616
It's not this, I change the Promise I have and worked, I don't know what the error is in Axios, but when I redeploy worked, the axios is right, this headers is custom there is no problem with my axios call, but thank you so much
Brody
Brody2y ago
okay so it was a code issue either way glad its working now!
lucaszdevmmyers616
Yes, is something about the Promise, in the catch of the Promise I wasn't using the res.status...
Brody
Brody2y ago
mmm mixing await with promises yummy wrapped in a big try / catch block
Finn
Finn2y ago
await is a keyword that resolved promises though an .then .catch is not good though
Brody
Brody2y ago
Finn you know what I mean
Finn
Finn2y ago
i do and here are some links around handling promises if you want @Lucas Lima https://www.youtube.com/watch?v=ITogH7lJTyE https://trytm.vercel.app/
lucaszdevmmyers616
not work the promises is to the map await until end to then return if I do without promises the array is send empty
Finn
Finn2y ago
can you paste the code i can format it so it works (discord formatting so it will be slightly ugly looking ) ( i asume the issue is your resolving the promises then trying to send a request back, but the request is closed )
lucaszdevmmyers616
try { const lotteriesArray = []; const promises = lotteries.map(async (lottery, index) => { const response = await lotteryApi.getLottery(lottery); lotteriesArray[index] = { lottery_name: response.nome, amount: response.valor_estimado_proximo_concurso, color: lotteryColors[index], date: response.data_concurso, tender_number: response.numero_concurso, description: lotteryDescription[index] }; }); Promise.all(promises).then(() => { res.status(200).json({ status: 200, lotteries: lotteriesArray }); }).catch(error => { res.status(500).json({ error: "Server error, please try again later." }); }); } catch (error) { res.status(500).json({ error: "Server error, please try again later." }); } the all code
Finn
Finn2y ago
try {
const promises = lotteries.map(async (lottery, index) => {
const response = await lotteryApi.getLottery(lottery);

return {
lottery_name: response.nome,
amount: response.valor_estimado_proximo_concurso,
color: lotteryColors[index],
date: response.data_concurso,
tender_number: response.numero_concurso,
description: lotteryDescription[index]
};
});

const lotteries = await Promise.all(promises)
res.json({
status: 200,
lotteries: lotteries
})

} catch (error) {
res.status(500).json({
error: "Server error, please try again later."
});
}
try {
const promises = lotteries.map(async (lottery, index) => {
const response = await lotteryApi.getLottery(lottery);

return {
lottery_name: response.nome,
amount: response.valor_estimado_proximo_concurso,
color: lotteryColors[index],
date: response.data_concurso,
tender_number: response.numero_concurso,
description: lotteryDescription[index]
};
});

const lotteries = await Promise.all(promises)
res.json({
status: 200,
lotteries: lotteries
})

} catch (error) {
res.status(500).json({
error: "Server error, please try again later."
});
}
somthing like that? oh wait 1 sec that worke @Lucas Lima i must admit ive never used promise.all
Brody
Brody2y ago
isn't there a way to do error handling without encapsulating everything in a try block?
Finn
Finn2y ago
ya
Brody
Brody2y ago
Example, because I would like to know too
Finn
Finn2y ago
using or the like
export const to = async <T>(
promise: Promise<T>,
): Promise<[T, null] | [null, Error]> => {
try {
return [await promise, null];
} catch (e) {
return [null, e instanceof Error ? e new Error(String(e))];
}
};
export const to = async <T>(
promise: Promise<T>,
): Promise<[T, null] | [null, Error]> => {
try {
return [await promise, null];
} catch (e) {
return [null, e instanceof Error ? e new Error(String(e))];
}
};
( altho i would not have that last line ) fixed
Brody
Brody2y ago
That's still try catch, or is that just the extent of JavaScript's error handling?
Finn
Finn2y ago
nah the idea is you wrap the function that throws with to and yes this is the extent. like
const [res, error] = await to(
fetch("/myThing")
.then(async r => zodSchema.parse(await r.json())
)

if (error) return console.log("error yes")

... do more things
const [res, error] = await to(
fetch("/myThing")
.then(async r => zodSchema.parse(await r.json())
)

if (error) return console.log("error yes")

... do more things
error handling in ts is BAD
Brody
Brody2y ago
Hey that look like Go
Finn
Finn2y ago
p much. and thats my goto solution
Brody
Brody2y ago
I like that example
Finn
Finn2y ago
which example
Brody
Brody2y ago
The one that looks like Go
Finn
Finn2y ago
this?
Brody
Brody2y ago
^
Finn
Finn2y ago
@Lucas Lima let us know if it worked or not when you are back ah yea that
Brody
Brody2y ago
I will yoink for later use, thank you
Finn
Finn2y ago
nw. make sure you use zod aswell!
Brody
Brody2y ago
I don't do typescript
Finn
Finn2y ago
oh and it requires ts 4.6 > to get the inference union working properly
Brody
Brody2y ago
Typescript is for people with wrinkly brains
Finn
Finn2y ago
( thats a compliment lol ) did you mean that
Brody
Brody2y ago
Yes, I'm smooth brain so everything I do is vanilla
Finn
Finn2y ago
theowat
Brody
Brody2y ago
everything
Want results from more Discord servers?
Add your server