mongodb mongoose 未定义连接数据库
mongodb connection db is undefined with mongoose
我通过以下方式使用 Mongoose 进行连接
import { createConnection } from 'mongoose';
this.m_context = createConnection('mongodb://localhost/master')
但是当我尝试访问 this.m_context.db
时,它给了我未定义的信息。
我在这里做错了什么?我检查了在指南针中工作正常的连接字符串。
我发现,createConnection 没有打开连接,因此,它给出了未定义的信息,因为它仅在连接打开时才提供连接数据库信息。
this.m_context = await new Promise<Connection>((resolve) => {
createConnection('mongodb://localhost/master', undefined, (error, result) => {
resolve(result)
});
});
我通过以下方式使用 Mongoose 进行连接
import { createConnection } from 'mongoose';
this.m_context = createConnection('mongodb://localhost/master')
但是当我尝试访问 this.m_context.db
时,它给了我未定义的信息。
我在这里做错了什么?我检查了在指南针中工作正常的连接字符串。
我发现,createConnection 没有打开连接,因此,它给出了未定义的信息,因为它仅在连接打开时才提供连接数据库信息。
this.m_context = await new Promise<Connection>((resolve) => {
createConnection('mongodb://localhost/master', undefined, (error, result) => {
resolve(result)
});
});