本文介绍了节点 js 错误:Mongoose 与 MondoDB 连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置我的连接后

const mongoose = require('mongoose')

const Post = require('./database/models/Post')


mongoose.connect("mongodb://localhost/testdb", {useNewUrlParser: "true", useUnifiedTopology: true})

和我的服务器页面如下:

and my server page as below:

const mongoose = require('mongoose')const app = new express()

const mongoose = require('mongoose')const app = new express()

//mongoose.connect('mongodb://localhost/node-js-blog')
//mongoose.connect("mongodb://localhost:27017/node-js-blog", {useNewUrlParser: "true",})
mongoose.connect('mongodb://localhost/node-js-blog', {useNewUrlParser: "true", useUnifiedTopology: true})

我在测试时遇到这个错误

i get this error while testing

在此处输入图片说明

(node:8744) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017在新的 MongooseServerSelectionError (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\error\serverSelection.js:22:11)在 NativeConnection.Connection.openUri (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\connection.js:823:32)在 Mongoose.connect (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\index.js:333:15)在对象.(C:\Users\Globalwise\Desktop\nodejs-blog\test.js:6:10)在 Module._compile (internal/modules/cjs/loader.js:701:30)在 Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)在 Module.load (internal/modules/cjs/loader.js:600:32)在 tryModuleLoad (internal/modules/cjs/loader.js:539:12)在 Function.Module._load (internal/modules/cjs/loader.js:531:3)在 Function.Module.runMain (internal/modules/cjs/loader.js:754:12)启动时(内部/bootstrap/node.js:283:19)在 bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)(节点:8744)UnhandledPromiseRejectionWarning:未处理的承诺拒绝.此错误源于抛出异步函数内部没有 catch 块,或者拒绝一个没有用 .catch() 处理过的承诺.(拒绝编号:1)(节点:8744)[DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝.将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程.

(node:8744) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 at new MongooseServerSelectionError (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\error\serverSelection.js:22:11) at NativeConnection.Connection.openUri (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\connection.js:823:32) at Mongoose.connect (C:\Users\Globalwise\Desktop\nodejs-blog\node_modules\mongoose\lib\index.js:333:15) at Object. (C:\Users\Globalwise\Desktop\nodejs-blog\test.js:6:10) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) at Function.Module.runMain (internal/modules/cjs/loader.js:754:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)(node:8744) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async functionwithout a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)(node:8744) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

请帮忙,因为我花了几天时间调试这个.

please help as i have spent days debugging this.

提前致谢.

推荐答案

您的 MongoDB 服务器是否正在运行?

Is your MongoDB server running?

这篇关于节点 js 错误:Mongoose 与 MondoDB 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 14:22