我正在使用Node.js服务器,它连接到MongoDB并使用TypeScript编写。当我尝试使用MongoDB时,它不会创建连接,但是当我检查mongo输出时,它的确会创建连接。
在Node.js中定义连接的代码中缺少什么?
我的连接字符串为'mongodb://localhost:27017'
我的连接方法:
connect() {
console.log('connecting to mongo') //this is called
MongoClient.connect(this.connectionString, {useNewUrlParser: true})
.then(client => {
console.log('setting client'); //this doesn't get called
this.client = client;
console.log(this.client);
})
.catch(error => {
console.log('error during connecting to mongo: '); //this also doesn't get called
console.error(error);
});
}
Mongo输出:
2018-11-08T23:06:24.106 + 0100我接受127.0.0.1:51345#11的NETWORK [listener]连接(2个连接现已打开)
2018-11-08T23:06:24.107 + 0100我网络[conn11]从127.0.0.1:51345 conn11接收了客户端元数据:{驱动程序:{名称:“ nodejs”,版本:“ 3.1.9”},操作系统:{类型:“ Darwin”,名称:“ darwin”,体系结构:“ x64”,版本:“ 17.7.0”},平台:“ Node.js v8.9.3,LE,mongodb-core:3.1.8”}
我的存储库位于https://github.com/FrisoDenijs/MEAN-ToDo/blob/master/server/src/db/mongo.db.ts,以获取完整的代码。
shpper要求的 console.log(db)
MongoClient {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s:
{ url: 'mongodb://localhost:27017',
options:
{ servers: [Array],
caseTranslate: true,
useNewUrlParser: true,
socketTimeoutMS: 360000,
connectTimeoutMS: 30000,
promiseLibrary: [Function: Promise] },
promiseLibrary: [Function: Promise],
dbCache: {},
sessions: [] },
topology:
Server {
domain: null,
_events:
{ serverOpening: [Function],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHeartbeatFailed: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function],
commandStarted: [Function],
commandSucceeded: [Function],
commandFailed: [Function],
joined: [Function],
left: [Function],
ping: [Function],
ha: [Function],
authenticated: [Function],
error: [Function],
timeout: [Function],
close: [Function],
parseError: [Function],
open: [Object],
fullsetup: [Object],
all: [Object],
reconnect: [Function] },
_eventsCount: 25,
_maxListeners: Infinity,
clientInfo:
{ driver: [Object],
os: [Object],
platform: 'Node.js v8.9.3, LE' },
s:
{ coreTopology: [Object],
sCapabilities: null,
clonedOptions: [Object],
reconnect: true,
emitError: true,
poolSize: 5,
storeOptions: [Object],
store: [Object],
host: 'localhost',
port: 27017,
options: [Object],
sessionPool: [Object],
sessions: [],
promiseLibrary: [Function: Promise] } } }
答案 0 :(得分:1)
根据您所说的关于既不调用错误也不调用成功回调的说法,我认为问题不仅在于以下代码:https://github.com/FrisoDenijs/MEAN-ToDo/blob/master/server/src/db/mongo.db.ts
而且例如此处:https://github.com/FrisoDenijs/MEAN-ToDo/blob/master/server/src/controllers/to-do.controller.ts
当您运行for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
.................
时,应该有一个选项来等待mongo.connect
返回的承诺。因此,我将MongoClient.connect
中的代码更改为例如这样(取决于您如何处理此承诺):
mongo.db.ts
然后在connect() {
console.log('connecting to mongo')
return MongoClient.connect(this.connectionString, {useNewUrlParser: true})
.then(client => {
console.log('setting client');
this.client = client;
console.log(this.client);
})
.catch(error => {
console.log('error during connecting to mongo: ');
console.error(error);
});
}
中,您可以to-do.controller.ts
或使用await
:
then
我想您的代码只是不“等待”建立连接,然后失败。
答案 1 :(得分:1)
问题是因为mongo.connect
是异步代码,并且控制器错误地调用它。因此后续行mongo.getDb()
将在没有正确启动this.client
的情况下执行。
由于您使用的是Typescript,因此我们可以使用async/await
来获得更清晰的代码。
async connect() { // add async
console.log('connecting to mongo');
try {
if (!this.client) { // I added this extra check
console.log('setting client');
this.client = await MongoClient.connect(this.connectionString, { useNewUrlParser: true })
console.log(this.client);
}
} catch(error) {
console.log('error during connecting to mongo: ');
console.error(error);
}
}
并在控制器代码中
async get(req: Request, res: Response) { // add async
const mongo = new MongoDb();
await mongo.connect(); // add await
const db = mongo.getDb();
// ....
}
我尝试了您的回购并进行了更改
希望有帮助
答案 2 :(得分:1)
以上解决方案确实有效,但是还有另一种我更喜欢的方法。它更像是一个标准的HTTP调用(无论是否有异步等待,我都可以使用它,但是最好保留它):
private async connect(){
this.mongo = new mongodb.MongoClient(this.url, {useNewParser: true});
await this.mongo.connect((err, client) => {
if(client.isConnected()){
console.log('setting client');
this.client = client;
console.log(this.client);
}
else{
console.log('error during connecting to mongo: ');
console.error(error);
}
});
}
这基于Node.js驱动程序api v3.2,您可以在此处查看: https://mongodb.github.io/node-mongodb-native/3.2/api/index.html
这可能只是剃猫的另一种方式,但我认为值得以后再来这里的其他人分享。