猫鼬与数据库的连接代码不起作用。 => 猫鼬.connection.once()

时间:2021-01-22 06:48:06

标签: javascript node.js mongodb mongoose backend

当需要将 mongoose 与数据库连接时,我有一个简单的连接代码,但它不起作用。 我不明白为什么?

第一部分代码。导入依赖

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

第二部分代码

//Config Object
const config = { useNewUrlParser: true, useUnifiedTopology: true };
//Create express app
const app = express();

//Set URI
const URI = 'mongodb://localhost/name'

//Database
mongoose.connect(URI, config);

//Store Connection Object
const db = mongoose.connection;

db.once("open", () => {
    console.log("Connected to MongoDB database...");
});

错误

(node:16484) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\connection.js:845:32)
    at C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\index.js:345:10
    at C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\index.js:1135:10)
    at Mongoose.connect (C:\NODEJS\quotes-api-men\node_modules\mongoose\lib\index.js:344:20)
    at Object.<anonymous> (C:\NODEJS\quotes-api-men\app.js:15:10)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
(node:16484) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a p
romise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.
html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16484) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a no
n-zero exit code.

有没有人遇到过同样的问题??

2 个答案:

答案 0 :(得分:0)

是因为mongoose.connect返回的promise处理不当。如果连接成功,它将返回一个连接对象(这意味着您的应用程序已成功连接到您的 Mongo 数据库),否则它将返回一个您需要使用 catch 块处理的错误。 所以代码看起来像:

mongoose.connect(URL, config)
.then(connection_obj => {
    // things you want to do with successful connection
}).catch(err => {
    // handle the error or log "err" object to debug
});

有关详细信息,您可以查看 mongoose error handling

的文档

答案 1 :(得分:0)

出现这个问题是因为我的本地电脑没有安装 MongoDB 服务。

当我在本地计算机上安装 MongoDB 服务后,问题就解决了。

另外,如果有人遇到同样的问题来检查服务 MongoDB,可能没有在您的本地机器上启动,您需要启动此服务。

按照以下步骤启动或检查启动与否:

转到控制面板并点击管理工具。 双击服务。一个新窗口打开。 搜索 MongoDB.exe。右键单击它并选择开始。 服务器将启动。现在再次执行 npm start ,这次代码可能会工作。