2021年2月3日星期三

Extra text is getting added when calling route from react

I have created a route to return data from mongo which I have tested in postman and it works as expected however when i try to call it from my react frontend extra text is getting added to the route so I am getting a 404.

My backend route looks like this:

router.get("/password-reset-token/:user_id", async (req, res) => {      try {          const userId = req.params.user_id;          const passwordResetTokens = await PasswordResetToken.find({ userId }).sort({              validTill: -1,          });          return res.status(200).json(passwordResetTokens[0]);      } catch (err) {          console.error(err.message);          return res.status(500).send("Server error");      }  });  

My method for calling the route looks like this:

export const getPasswordResetToken = async (user_id) => {      const config = {          headers: {              "Content-Type": "application/json",          },      };      try {          await axios.get(`api/auth/password-reset-token/${user_id}`, config);      } catch (err) {          console.log(err);      }  };  

and I have tried to call this method in a react functional component like so:

import it

import { getPasswordResetToken } from "../../api/index";  

put it in a method

const test = async (e) => {      e.preventDefault();      try {          getPasswordResetToken(match.params.user_id);      } catch (error) {          console.log(error);      }  };  

call it in an onclick

<button onClick={(e) => test(e)}>test me</button>  

But when I run the app and click the "test me" button I get a 404 as it tries to access GET http://localhost:3000/update-password/api/auth/password-reset-token/5fec962aabc5c20640dc309f

I am not sure whhy the update-password is coming from so any help would be appreciated :)

https://stackoverflow.com/questions/66037087/extra-text-is-getting-added-when-calling-route-from-react February 04, 2021 at 07:10AM

没有评论:

发表评论