【发布时间】:2020-07-20 13:51:35
【问题描述】:
我想在 mongoose 中使用事务,因此我在开发阶段安装了 run-rs 来测试它。根据像这样的指南https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 run-rs 在本地主机上启动它。当我通过终端运行run-rs -v 4.2.5 --keep --shell 启动副本集时,它显示:在“mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs”上启动副本集。为什么它为我说 DESKTOP 而不是 localhost,我将如何在 mongoose.connect() 中连接到它?
我在 index.js 中尝试了以下内容:
const app = express();
const PORT = 27017;
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
mongoose.Promise = global.Promise;
mongoose.connect(dbUri, {
replicaSet: 'rs',
useNewUrlParser: true,
useUnifiedTopology: true
});
另外我将如何附加数据库名称?
更新:
PS C:\Users\Workstation\project> npm start
> project@1.0.0 start C:\Users\Workstation\project
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Your server is running on Port 4000
(node:7436) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
at new MongooseServerSelectionError (C:\Users\Workstation\project\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Workstation\ project\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\Workstation\ project\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:/Users/Workstation/project/index.js:22:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at loader (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at Object.<anonymous> (C:\Users\Workstation\project\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
(node:7436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7436) [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.
更新 我接受了下面的答案,因为用于连接的字符串有效,因此解决了原始问题。但是我切换到 Linux,它现在无需进一步设置即可工作。 run-rs 似乎在我的 Windows 机器上不起作用。
【问题讨论】:
标签: node.js mongodb mongoose replicaset