简体   繁体   English

为什么mongodb无法创建新数据库?

[英]Why mongodb won't create a new database?

Whenever I try to register a new user on my website or search for something stored in the database the connection can't get through to the database. 每当我尝试在我的网站上注册新用户或搜索数据库中存储的内容时,连接都无法连接到数据库。 I am using node.js and MongoDB. 我正在使用node.js和MongoDB。 This is how I configured the connection to the server in app.js 这是我在app.js配置与服务器的连接的方式

mongo.connect("mongodb://localhost:27017/Tuskdb", { useNewUrlParser: true });

When I check the mongo console for databases with the command show dbs I don't get my database listed. 当我使用show dbs命令检查mongo控制台中的数据库时,没有列出我的数据库。 However, when I run my server using nodemon app.js I get no errors related to the database even though the connection doesn't go through. 但是,当我使用nodemon app.js运行服务器时,即使连接未通过,也不会出现与数据库相关的错误。 It just gives me a 它给我一个

This page isn't working 此页面无效

localhost didn't send any data. 本地主机未发送任何数据。

ERR_EMPTY_RESPONSE ERR_EMPTY_RESPONSE

in the browser, but nothing in the console. 在浏览器中,但在控制台中什么也没有。 When I try to create a new database by changing my route to say : 当我尝试通过更改路线来创建新数据库时:

mongo.connect("mongodb://localhost:27017/newdb", { useNewUrlParser: true });

It won't do it. 它不会做。 What do you think happened? 您认为发生了什么事? I am not sure what kind of additional information I need to provide, so let me know in the comments. 我不确定需要提供哪种其他信息,因此请在评论中告诉我。

first you have to add error call back function an print it 首先,您必须添加错误回调函数才能打印出来

mongoose.connect(`"mongodb://localhost:27017/Tuskdb"`, function(error) {
    if(error){
        console.log("error in creating connection",error);
    }else{
    console.log(`monogodb connected)
    }
  });   

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

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