我收到错误:无法连接到 [未定义:27017]

I am getting Error: failed to connect to [undefined:27017]

本文关键字:未定义 27017 连接 错误      更新时间:2023-09-26

不知道该怎么做,当我在 git shell 中运行>>节点索引.js时无法连接。 它给了我以下内容

英雄联盟角色扮演游戏在端口 8080 上启动连接错误: [错误: 无法连接到 [未定义:27017]]

/* ==== MONGODB ==== */
var mongoose    = require('mongoose');
var db          = require('./config/db.js');
mongoose.connect(db.url);
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function() { console.log("Mongo DB connected!"); });
 /* ==== config/db.js ==== */
module.exports = "mongodb://<username>:<username>@ds052837.mongolab.com:52837/lolrpg"; 

这里的问题是代码第一部分中的db变量引用了连接字符串,但您尝试访问其上的 url 属性,最终未定义。

mongoose.connect(db.url)替换为 mongoose.connect(db)

或者,在 db.js 中,您可以将module.exports = ...替换为 module.exports.url = ...