skip to Main Content

I am trying to POST to an endpoint and update a users profile with a collection that is associated with their USERID. I keep getting a 401 unathorized error

THE ROUTER:

router.post(`/newfixture/:id`, async (req, res) => {
  let fixture = new Fixture({
    id: req.body.id,
    homeTeam: req.body.homeTeam,
    awayTeam: req.body.awayTeam,
    date: req.body.date,
    time: req.body.time,
    venue: req.body.venue,
  });
  // save the fixture to the user and return a response
  fixture = await fixture.save({
    id: req.body.id,
    homeTeam: req.body.homeTeam,
    awayTeam: req.body.awayTeam,
    date: req.body.date,
    time: req.body.time,
    venue: req.body.venue,
  });
  if (!fixture) {
    return res.status(404).send("The fixture cannot be created!");
  }
  res.send(fixture);
  console.log("fixture created", fixture);
});

Then the call to axios:

    axios
      .post(`${baseURL}users/fixtures/newfixture/${id}`, {
        homeTeam: homeTeam,
        awayTeam: awayTeam,
        venue: venue,
        date: date,
        time: time,
      })
      .then(res => {
        if (res.status === 200) {
          console.log('🤣 🤣 🤣 🤣 🤣 FIXTURE PUT', userProfile);
        } else {
          console.log('🤣 🤣 🤣 🤣 🤣 NOPE ', userProfile);
        }
      });

Also adding the middleware here

const userRoute = require("./routers/users");
app.use(`${api}/users`, userRoute);
mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority

Please can you help me?

2

Answers


  1. Chosen as BEST ANSWER

    Holy shit the problem was so stupid. I was declaring the authJWT from another file. Then I was redeclaring it in the middleware what an absolute ridiculous issue.

    I was redeclaring it here.

    // Middleware
    app.use(express.json());
    app.use(morgan("tiny"));
    app.use(errorHandler);
    

  2. I believe your endpoint is incorrect

    .post(`${baseURL}users/fixtures/newfixture/${id}`
    

    it should probably be

    .post(`${baseURL}/users/fixtures/newfixture/${id}`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search