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
Holy shit the problem was so stupid. I was declaring the
authJWT
from another file. Then I was redeclaring it in themiddleware
what an absolute ridiculous issue.I was redeclaring it here.
I believe your endpoint is incorrect
it should probably be