This is the code I have currently in my server folder. I was just wondering for some reason the catch console log is still sending even though the .then statement actively sends a response though res.send works and sends a response to the user?
const express = require("express");
const router = express.Router();
const { API_KEY, API_URL } = process.env
const axios = require("axios")
router.get("/", async (req, res) => {
console.log(API_URL + API_KEY)
await axios.get(API_URL + API_KEY + "&count=9")
.then((response) => {
res.send(response.data)
})
.catch(
console.log("error")
)
})
module.exports = router
Thank you!
.catch accepts a callback - right now, you're invoking console.log immediately and passing the result to .catch. This:
// ...
.catch(console.log("error"))
is equivalent to
const consoleResult = console.log("error");
// ...
.catch(consoleResult)
You need to change to
.catch(
() => {
console.log("error")
}
)
Or, even better, examine the argument to see what the error was:
.catch(
(error) => {
console.log("error:", error.message)
}
)