meku
meku
KPCKevin Powell - Community
Created by meku on 7/16/2023 in #front-end
Need help with MaterialUI's Grid component
2 replies
KPCKevin Powell - Community
Created by meku on 3/13/2023 in #back-end
I don't understand error handling middlewares (Express)
I'm learning from fullstackopen.com and now I'm going into error handling middlewares. This is their demo code for this part : https://github.com/fullstack-hy2020/part3-notes-backend/blob/part3-5/index.js My understanding is that middlewares need to have next() for the next middleware to be executed. Why the demo code shows ...
const errorHandler = (error, request, response, next) => {
console.error(error.message)

if (error.name === 'CastError') {
return response.status(400).send({ error: 'malformatted id' })
}

next(error)
}

const unknownEndpoint = (request, response) => {
response.status(404).send({ error: 'unknown endpoint' })
}

app.use(unknownEndpoint)
app.use(errorHandler)
const errorHandler = (error, request, response, next) => {
console.error(error.message)

if (error.name === 'CastError') {
return response.status(400).send({ error: 'malformatted id' })
}

next(error)
}

const unknownEndpoint = (request, response) => {
response.status(404).send({ error: 'unknown endpoint' })
}

app.use(unknownEndpoint)
app.use(errorHandler)
while there's no next() in unknownEndpoint middleware. How can errorHandler middleware be executed then?
6 replies
KPCKevin Powell - Community
Created by meku on 3/4/2023 in #front-end
Fetching data from an API and putting them inside an array but it works differently in browser/IDE?
It's React. Something weird happens. When I reload directly from the browser, it still logs an empty array, but when I save the file from VSCode, it displays the array just like I wanted.
const [country, setCountry] = useState([]);

useEffect(() => {
axios
.get("https://restcountries.com/v3.1/all")
.then((response) => {
const data = response.data;
const names = data.map((n) => n.name.common);
setCountry(names);
console.log(country);
})
.catch((error) => {
console.log(error);
});
}, []);
const [country, setCountry] = useState([]);

useEffect(() => {
axios
.get("https://restcountries.com/v3.1/all")
.then((response) => {
const data = response.data;
const names = data.map((n) => n.name.common);
setCountry(names);
console.log(country);
})
.catch((error) => {
console.log(error);
});
}, []);
4 replies