GusiiSenpaii
GusiiSenpaii

Reputation: 13

ExpressJS returns 500 code all the time when trying to send some data

I'm actually working on an API using web-scrapping, but I have a problem that I can't understand.

I have the following route for a GET request:

const router = require("express").Router();
const axios = require("axios");
const cheerio = require("cheerio");

router.get("/:id", async (req, res) => {
  const currency = req.params.id;
  try {
    const res = await axios.get(
      `https://coinmarketcap.com/currencies/${currency}/`
    );
    const html = res.data;
    var $ = cheerio.load(html);
    const price = $(".statsValue").first().text();
    const data = {
      market: Number(price.slice(1).replaceAll(",", "")),
    };
    console.log(data);
    res.status(200).json(data);
  } catch (error) {
    res.status(500).json(error);
  }
});

module.exports = router;

When I make a request for this route, I just get the 500 status code, even knowing it has the data. When I call the API route, it logs this on the console:

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Server is running on port 5000
{ market: 454939957399 }

This means that it has the data, logs the data, and on the res.status(200).json(data) something fails.

I have the exact 2 lines of status and json response in another API call and it works perfectly, but I've been like 2 hours with this one for almost nothing.

I hope someone can help me <3

Upvotes: 0

Views: 31

Answers (1)

robertklep
robertklep

Reputation: 203494

You're overwriting the res argument by a new variable:

const res = await axios.get(…);

Upvotes: 1

Related Questions