简体   繁体   English

mongoDB connect(通过Node.js驱动程序)不会给我控制台日志消息

[英]mongoDB connect (via Node.js driver) doesn't give me console log msg

I have this code on my server.js file. 我在server.js文件上有这个代码。 I want to connect to this mongoDB data base and then get a console log indication. 我想连接到这个mongoDB数据库,然后获得一个控制台日志指示。

However, there is no console log message for the database connection (for the server I do get log message). 但是,没有用于数据库连接的控制台日志消息(对于服务器,我获取日志消息)。 I don't get even an error message, just one message to the console log that the http is connected (nothing on the database). 我甚至没有收到错误消息,只有一条消息到控制台日志,http已连接(数据库上没有任何内容)。

why? 为什么? my code is below thanks 我的代码谢谢

server.js server.js

var express = require('express');
var app = express();
var http = require('http').Server(app); 
var io = require('socket.io')(http);
var path = require('path');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');

 //more code that is not relevant



var url = 'mongodb://1.0.0.127:4444/small-talkz';

MongoClient.connect(url, function(err, db) {
    console.log("Connected correctly to DB.");
    db.close();
});

http.listen((process.env.PORT || 3000), function(){
    console.log('listening on *:3000 '+ __dirname);
});

As noted in the docs of the Node.js driver, there is a default retry count of 5 and 5000 ms between each retry. 如Node.js驱动程序的文档中所述,每次重试之间的默认重试次数为5和5000 ms。 So you have to wait a bit before you actually get an error in your callback. 因此,在回调中实际出现错误之前,您必须等待一段时间。 As you currently do not write the error to console , you won't see the error but get an error due to accessing db which is then null . 由于您当前没有将错误写入console ,因此您将看不到错误,但由于访问db而获得错误,然后该数据为null

Change it to 将其更改为

MongoClient.connect(url, function(err, db) { 
    if (err) {
        console.log(err);
    }
    else {
        console.log("Connected correctly to DB.");
        db.close();
    }
});

and wait for a minute and you will see the error. 等一下,你会看到错误。 Alternatively, decrease the retry count or the delay between each retry to see it faster failing :) 或者,减少重试次数或每次重试之间的延迟,以便更快地看到它失败:)

After you see the error you either will be able to see the reason and fix it (probably your URI is wrong) or at least you can provide us with more information. 看到错误后,您将能够看到原因并修复它(可能是您的URI错误)或者至少您可以向我们提供更多信息。

Try with this to make connection. 尝试使用此连接。 Run mongod with the default port 27017 & localhost. 使用默认端口27017和localhost运行mongod。

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect("mongodb://localhost:27017/table_name", function(err, db) {
    if(err)
    {
      console.log(err);
    }
    else{
      console.log("Connected correctly to DB.");
      db.close();
    }
  });
});

If its working with this, then make sure you are running mongo server on specific ip 1.0.0.127(weird ip) and on port 4444. 如果它正在使用它,那么请确保您在特定的IP 1.0.0.127(奇怪的IP)和端口4444上运行mongo服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM