Roffy bc
Roffy bc

Reputation: 91

Axios.get is not returning anything in nodeJS,Express

I have a problem with .get request. Somehow it is not returning anything? (GET http://localhost:8080/admin net::ERR_EMPTY_RESPONSE) Any suggestions?

Get Route,With this I'm trying to filter all items by their username:

   app.get("/:username", verify, (req, res) => {
  console.log("Welcome to roffys server");
  Todo.find({ username: req.params.username }).then((err, todo) => {
    if (err) {
      console.log("Error retrieving todos");
    } else {
      res.json(todo);
    }
  });
});

Verify function,here I'm verifying my auth-token,I console logged it and it is working fine:

    const jwt = require("jsonwebtoken");
module.exports = function (req, res, next) {
  const token = req.header("auth-token");
  console.log("-----token", token);
  if (!token) return res.status(401).send("Access Denied");
  try {
    const verified = jwt.verify(token, "secretkey");
    req.user = verified;
  } catch (err) {
    res.status(400).send("Invalid token");
    next();
  }
};

FE Side with ReactJS :

 componentDidMount() {
const { getAll, setPageCount } = this.props.actions;
axios
  .get(`http://localhost:8080/${localStorage.getItem("username")}`, {
    headers: {
      "auth-token": localStorage.getItem("auth-token"),
    },
  })
  .then((res) => {
    getAll(res.data);
    setPageCount();
    console.log("--------res.data", res.data);
  })
  .catch((err) => {
    console.log("err", err);
  });
  }

Upvotes: 1

Views: 72

Answers (1)

Osokin Vadim
Osokin Vadim

Reputation: 31

app.get("/:username", verify, (req, res, next) => {
  console.log("Welcome to roffys server");
  Todo.find({ username: req.params.username }).then((err, todo) => {
    if (err) {
      console.log("Error retrieving todos");
      return next(err);
    } else {
      res.json(todo);
    }
  });
});

try to add next to your handler and call it when you receive an error.

Upvotes: 1

Related Questions