簡體   English   中英

在快遞會話中使用“商店”字段時獲取“UnhandledPromiseRejectionWarning”

[英]Getting 'UnhandledPromiseRejectionWarning' when using the 'store' field in express-session

我使用'express-session'在我的網站上啟用持久登錄。 一切都運行良好,但當我使用外部數據庫存儲cookie時,我收到以下警告:( (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 ha non-zero exit code. (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 ha non-zero exit code.

我已將其縮小為與session對象中的store字段相關。 您將在下面看到我的app.use(session{...}) 如果我從此JSON對象中刪除store字段,則默認使用MemoryStore並且不會拋出上述警告。 我正在使用IBM Cloudant DB,它可以完美地進行通信並按預期保存會話數據。 問題是上面的警告被拋出。

// 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
    }
}));

這是我的Cloudant設置,它完美運行並始終返回Session connected ,因為它正確連接。

// Create the session storage object to store cookies.
const store = new CloudantStore({
    url: CLOUDANT_DB_URL,
    database: DB_NAME
});

// Inform us that the session is connected to the cloudant database.
store.on('connect', () => console.log('Session connected'));
// Inform us that the session disconnected from the cloudant database.
store.on('disconnect', () => console.log('Session disconnected.'));
// Inform us that the session failed to connect to the cloudant database.
store.on('error', (err) => console.log(err.msg));

預期的結果是會話在沒有拋出上述警告的情況下工作。

我不認為它與您的代碼有任何關系,很可能它是您正在使用的庫。

錯誤是因為承諾是在沒有捕獲的情況下編寫的。 如果您不熟悉承諾,我建議您先了解它們的工作原理。

目前,如果承諾失敗並且沒有捕獲它永遠不會解決,這可能不會使程序崩潰,但會阻止它正常繼續。

一些程序員將.catch()從他們從未期望失敗的承諾中解放出來。 這不是一個好的做法,並且在未來的Node版本中,它將阻止帶有缺失捕獲的代碼運行。 現在你很好,但你應該弄清楚是什么庫導致了這個錯誤,並提醒作者,以便它可以更新。

如果你想自己修復它,就像添加:

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

對於承諾,請記住,這不會解決與未返回預期數據的承諾相關的問題,但您也可以添加邏輯以了解如何處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM