在快速会话中使用“商店”字段时获取“ UnhandledPromiseRejectionWarning”

时间:2019-05-21 20:10:44

标签: node.js passport.js cloudant express-session

我正在使用“ express-session”在我的网站上启用持久登录。一切正常,但是当我使用外部数据库存储cookie时,会收到以下警告: lock()

我将其范围缩小到与 (node:6084) UnhandledPromiseRejectionWarning (node:6084) 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(). (rejection id: 1) (node:6084) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process wit h a non-zero exit code. 对象中的store字段相关。在下面,您将看到我的session的代码。如果我从此JSON对象中删除app.use(session{...})字段,则默认使用store,并且不会引发以上警告。我正在使用IBM Cloudant DB,它可以完美通信并按预期保存会话数据。问题是上面抛出的警告。

MemoryStore

这是我的Cloudant设置,它可以正常运行,并始终返回// Works, but warning is thrown app.use(session({ store: store, // Use the IBM Cloudant database as a store. resave: true, // Store sessions back to the store if they were never modified. saveUninitialized: true, // Save any uninitialized or new sessions that have no data. secret: SESSION_SECRET, cookie: { maxAge: 1000 * 60 * 60 * 2, secure: true } })); // Works with no warning being thrown. app.use(session({ resave: true, // Store sessions back to the store if they were never modified. saveUninitialized: true, // Save any uninitialized or new sessions that have no data. secret: SESSION_SECRET, cookie: { maxAge: 1000 * 60 * 60 * 2, secure: true } })); ,因为它可以正确连接。

Session connected

预期结果是会话正常工作,而不会引发以上警告。

1 个答案:

答案 0 :(得分:0)

我认为这与您的代码无关,很可能是您正在使用的库。

该错误是因为写的承诺没有成功。如果您不熟悉诺言,建议您先阅读一下诺言的工作原理。

当前,如果一个承诺失败并且没有成功,它将永远无法解决,这可能不会使您的程序崩溃,但会阻止它继续正常运行。

一些编码器从他们从未期望会失败的承诺中退出.catch()。这不是一个好习惯,在Node的未来版本中,它将阻止运行带有承诺丢失的捕获的代码。现在您还不错,但是您应该找出导致此错误的库,并警告作者,以便可以对其进行更新。

如果您想自己修复它,只需添加以下内容即可:

.catch((error)=>{console.log("The promise failed! error: "+error)})

对于诺言,请记住,这不会解决与诺言不返回预期数据有关的问题,但是您也可以添加逻辑来处理该问题。