【问题标题】:Getting "Error Slash in host identifier" When connecting to MongoDB with Mongoose and Express使用 Mongoose 和 Express 连接到 MongoDB 时出现“主机标识符中的错误斜线”
【发布时间】:2021-06-05 21:02:56
【问题描述】:

我使用 MongoDB 指令按照他们自己的文档从这里安装社区版:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/#std-label-osx-prereq

我从文档中的这个命令开始:

brew services start mongodb-community@4.4

这给了我这个输出:

==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

还要注意,如果我运行这个命令来验证:

Name                  Status  User  Plist
mongodb-community     error   blake /Users/blake/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
mongodb-community@4.2 stopped

它说状态是错误!所以我很困惑。为什么一开始说成功?

然后我在我的Express 应用程序中有这个小代码让我开始,这就是我得到另一个错误的地方:

/Users/blake/NewYork/auth/server/node_modules/mongodb/lib/url_parser.js:226 throw new Error('主机标识符中的斜线'); ^

Error: Slash in host identifier
    at parseConnectionString (/Users/blake/NewYork/auth/server/node_modules/mongodb/lib/url_parser.js:226:15)
    at parseHandler (/Users/blake/NewYork/auth/server/node_modules/mongodb/lib/url_parser.js:135:14)

这就是我尝试连接的方式:

const app = express();

// DB Setup
mongoose.connect("mongodb://localhost:auth/auth");

// App Setup
app.use(morgan("combined"));
app.use(bodyParser.json({ type: "*/*" }));
router(app);

// Server Setup
const port = process.env.port || 3090;
const server = http.createServer(app);
server.listen(port);
console.log("server listening on port ", port);

【问题讨论】:

    标签: node.js mongodb express


    【解决方案1】:

    不应该是mongodb://username:password@localhost:27017/auth

    您还可以使用以下查询参数提供单独的 authSource:?authSource=auth。这是包含用户凭据集合的数据库的名称。

    这会给我们:mongodb://username:password@localhost:27017/auth?authSource=auth

    末尾的/auth 应该是您要连接的数据库的名称。

    另外,将 27017 替换为您运行 mongo 的任何端口。

    More here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 2021-03-17
      • 2018-09-15
      • 2018-11-26
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多