MongoDB - 未连接到数据库和.catch函数的错误

时间:2022-10-28 12:59:13

The code below is what is wrong and it runs up until it gets to the .catch function with Mongo DB. And I can't figure out what is wrong. If i remove the catch and let it error it shall come up with this error -

下面的代码是错误的,它会运行,直到它到达Mongo DB的.catch函数。我无法弄清楚出了什么问题。如果我删除了捕获并让它出错它会出现这个错误 -

(node:72993) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

(node:72993)UnhandledPromiseRejectionWarning:未处理的promise拒绝(拒绝ID:1):MongoError:第一次连接时无法连接到服务器[localhost:27017] [MongoError:connect ECONNREFUSED 127.0.0.1:27017]

//Dependicies
var express = require("express");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var routes = require('./routes/route');
var app = express();


//MongoDB
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, function () {

console.log("Connected to Database")

}).catch(function () {
  console.log("Not Connected to Database ERROR!");


});


//Routes Listed
app.use('/', routes);


//Start Server
const defaultPortNumber = 8080
const port = process.env.PORT || defaultPortNumber

app.listen(port, err => console.log(err || `API is Running on port ${port}.`))

Added code to see what the error is actually saying via this link - How do I find out where an unhandled promise rejection occured?

添加了代码以通过此链接查看错误实际说明的内容 - 如何找出未处理的承诺拒绝发生的位置?

And Error now says - MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

而错误现在说 - MongoError:第一次连接时无法连接到服务器[localhost:27017] [MongoError:connect ECONNREFUSED 127.0.0.1:27017]

1 个解决方案

#1


0  

You are using some strange mix between ordinary callback and promise handling. When you are unable to connect, which is your problem, you get:

您在普通回调和承诺处理之间使用了一些奇怪的组合。当您无法连接时,这是您的问题,您会得到:

C:\test> node app
API is Running on port 8080.
Connected to Database
Not Connected to Database ERROR!

Because both the callback and the catch function are called.

因为调用了回调函数和catch函数。

If you want to handle the promise returned from connect you should use then() and catch() like this:

如果你想处理从connect返回的promise,你应该使用then()和catch(),如下所示:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }).then(() => {
    console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

Now it will choose either the then function or the catch function if the connect promise fails.

现在,如果connect promise失败,它将选择then函数或catch函数。

The actual error is ECONNREFUSED. It means your application is unable to connect to the database. Make sure mongod is running. You will get exactly that error message if it's not. If that's not the case then make sure the port mongod is running on is 27017, as that is the default port if you don't supply it in the connection url.

实际错误是ECONNREFUSED。这意味着您的应用程序无法连接到数据库。确保mongod正在运行。如果不是,您将得到完全错误的消息。如果不是这种情况,那么确保端口mongod正在运行是27017,因为如果你没有在连接URL中提供它,那么这是默认端口。

Finally, the UnhandledPromiseRejectionWarning message is shown when you either are missing the catch function or you're not handling the error in an ordinary callback.

最后,当您缺少catch函数或者您没有在普通回调中处理错误时,会显示UnhandledPromiseRejectionWarning消息。

E.g. for ordinary callback:

例如。对于普通回调:

This would give you UnhandledPromiseRejectionWarning:

这会给你UnhandledPromiseRejectionWarning:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, () => {
    console.log("Connected to Database");
})

This would not:

这不会:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, (err) => {
    if (err) throw err;
    console.log("Connected to Database");
})

#1


0  

You are using some strange mix between ordinary callback and promise handling. When you are unable to connect, which is your problem, you get:

您在普通回调和承诺处理之间使用了一些奇怪的组合。当您无法连接时,这是您的问题,您会得到:

C:\test> node app
API is Running on port 8080.
Connected to Database
Not Connected to Database ERROR!

Because both the callback and the catch function are called.

因为调用了回调函数和catch函数。

If you want to handle the promise returned from connect you should use then() and catch() like this:

如果你想处理从connect返回的promise,你应该使用then()和catch(),如下所示:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }).then(() => {
    console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

Now it will choose either the then function or the catch function if the connect promise fails.

现在,如果connect promise失败,它将选择then函数或catch函数。

The actual error is ECONNREFUSED. It means your application is unable to connect to the database. Make sure mongod is running. You will get exactly that error message if it's not. If that's not the case then make sure the port mongod is running on is 27017, as that is the default port if you don't supply it in the connection url.

实际错误是ECONNREFUSED。这意味着您的应用程序无法连接到数据库。确保mongod正在运行。如果不是,您将得到完全错误的消息。如果不是这种情况,那么确保端口mongod正在运行是27017,因为如果你没有在连接URL中提供它,那么这是默认端口。

Finally, the UnhandledPromiseRejectionWarning message is shown when you either are missing the catch function or you're not handling the error in an ordinary callback.

最后,当您缺少catch函数或者您没有在普通回调中处理错误时,会显示UnhandledPromiseRejectionWarning消息。

E.g. for ordinary callback:

例如。对于普通回调:

This would give you UnhandledPromiseRejectionWarning:

这会给你UnhandledPromiseRejectionWarning:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, () => {
    console.log("Connected to Database");
})

This would not:

这不会:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, (err) => {
    if (err) throw err;
    console.log("Connected to Database");
})