In React JS:
Here is my code:
....some code......
verifyTokenResults = await axios.post('http://localhost:5000/otp/token/verify', {accessToken: authToken})
Above, I am posting a token from React to the Node JS route below through request.body:
Here is my Endpoint in Node JS / Express JS:
router.post('/token/verify', async (request, response) =>{
const tokenToVerify = request.body.accessToken;
console.log(tokenToVerify);
let cachedToken;
let individualAccessToken;
let emailWithToken;
if (tokenToVerify) {
cachedToken = await accessTokenModel.find({accessToken: tokenToVerify}).sort({_id: -1}).limit(1);
//access the accessToken key of the returned json
for (let record of cachedToken) {
individualAccessToken = record["accessToken"];
emailWithToken = record["email"];
}
if (individualAccessToken === tokenToVerify) {
return response.status(200).json({message: `JWT Token Verified Successfully!`});
} else {
return response.status(404).json({message: `Invalid JWT Token!`});
}
}
});
**Here is the error that I was getting in the Node JS console, before wrapping my method with a try...catch:
I am getting the following error:**
(node:2252) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'accessToken' of undefined
(node:2252) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
When I wrapped the function with try...catch, In React does not move forward after it finishes with the request. it gives me a Preflight