我通過以下方式使用 Mongoose 進行連接
import { createConnection } from 'mongoose';
this.m_context = createConnection('mongodb://localhost/master')
但是當我嘗試訪問this.m_context.db它時,它給了我不確定性。
我在這里做錯了什么?我檢查了在指南針中作業正常的連接字串。
uj5u.com熱心網友回復:
我發現,createConnection 沒有打開連接,因此,它給出了未定義的,因為它僅在連接打開時才提供連接資料庫資訊。
this.m_context = await new Promise<Connection>((resolve) => {
createConnection('mongodb://localhost/master', undefined, (error, result) => {
resolve(result)
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/451915.html
下一篇:在JavaScript中解構元組
