UnhandledPromiseRejectionWarning:来自 Node JS 的未处理的承诺拒绝,React 中的预检响应

abc_123

在 React JS 中:

这是我的代码:

....some code......

verifyTokenResults = await axios.post('http://localhost:5000/otp/token/verify', {accessToken: authToken})

上面,我通过 request.body 将一个令牌从 React 发布到下面的 Node JS 路由:

这是我在 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!`});
        }
    }


  });

**这是我在 Node JS 控制台中遇到的错误,在使用 try...catch 包装我的方法之前:

我收到以下错误:**

(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)

当我用 try...catch 包装函数时,React 在完成请求后不会向前移动。它给了我一个预检

肯尼斯·卢

我假设您正在使用 Express。

为了在 express 中处理 JSON post 数据,您必须express.json()在您的应用程序上使用中间件。中间件解析 JSON,并将其转换为原生 js 对象,将其绑定到request.body.

然后,您可以像这样访问 JSON 的内容:

const tokenToVerify = request.body.accessToken

旁注:存储 JWT 服务器端完全违背了 JWT 的目的,即消除存储会话数据的需要。

相反,JWT 应该存储在客户端 localStorage 中。然后,在后续请求中,客户端在请求的授权标头中设置其访问令牌。

您还可以将 JWT 设置为 httpOnly cookie,访问令牌将与后续请求一起发送。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

UnhandledPromiseRejectionWarning:Node.JS中未处理的承诺拒绝(拒绝ID:1)

UnhandledPromiseRejectionWarning:未处理的承诺拒绝 - Node.js

Node.js未处理的承诺拒绝

缓存承诺导致Node.js中未处理的承诺被拒绝

如何在Node.js ExpressJS中的异步对象方法中处理未处理的承诺拒绝?

是什么确定在node.js(控制台/脚本)中未处理承诺拒绝?

未在Node.js中定义未处理的承诺拒绝错误值

Express.js UnhandledPromiseRejectionWarning:未处理的承诺拒绝

未处理的承诺拒绝来自 Try 中调用的承诺

(节点:20732)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来在Node.js中

如何找到未在Node.js UnhandledPromiseRejectionWarning中处理的承诺?

Node.js 处理来自链式承诺的响应

我的节点应用程序中的“UnhandledPromiseRejectionWarning:未处理的承诺拒绝”

UnhandledPromiseRejectionWarning:未处理的承诺拒绝 - API

UnhandledPromiseRejectionWarning:未处理的 API 承诺拒绝

XHR 请求:UnhandledPromiseRejectionWarning:未处理的承诺拒绝

Ionic 3 UnhandledPromiseRejectionWarning:未处理的承诺拒绝

Node.js未处理的拒绝错误

Node 服务器中未处理的承诺拒绝,仅在 Heroku 上,在本地工作

React Native 中未处理的承诺拒绝警告

捕获到的承诺中未处理的捕获错误-Node.js

在 BigQuery 中运行 node.js 文件时如何停止未处理的承诺错误?

如何将Node.js连接到现有的Postgres?错误:未处理的承诺被拒绝

如何解决“未处理的承诺拒绝:错误:无法解析来自http:// localhost:3000 /的裸指定符“ app.js”

UnhandledPromiseRejectionWarning:在处理某些问题时拒绝未处理的承诺

Node Js (Sequelize) - 未处理的拒绝 SequelizeDatabaseError:“字段列表”中的未知列“id”

如何处理Node.JS中的UnhandledPromiseRejectionWarning

Angular 2 中未处理的承诺拒绝

了解node.js中的承诺拒绝