【问题标题】:throw new Mongoose Error ('The ''uri'' parameter should be "openuri")抛出新的 Mongoose 错误(''uri'' 参数应该是“openuri”)
【发布时间】:2021-04-21 07:19:19
【问题描述】:

如果有人能理解我的问题,我浏览了很多帖子,但没有找到任何有用的内容

这是我的 index.js 文件

const express = require("express");
const app = express();
const mongoose = require("mongoose");

const dotenv = require("dotenv");
require("dotenv").config();

//Import Routes
const authRoute = require("./routes/auth");

//connnect to DB
mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true }, () =>
  console.log("Connected To DB!")
);

//Middlewares
app.use(express.json());

//route Middleware
app.use("/api/user", authRoute);
app.listen(3000, () => console.log("server up and Running"));

这是 .env 文件

DB_CONNECT = 'mongodb+srv://username:<password>@cluster0.5tnd3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
PORT = 3000

这是我在终端中遇到的错误

    throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
    ^

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
    at NativeConnection.Connection.openUri (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\connection.js:694:11)
    at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:350:10
    at promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:9:12)
    at Mongoose._promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:1154:10)
    at Mongoose.connect (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:349:20)
    at Object.<anonymous> (C:\COURSES\Authentication-nodeJS\index.js:12:10)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

(node:4364) UnhandledPromiseRejectionWarning: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
    at NativeConnection.Connection.openUri (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\connection.js:694:11)
    at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:350:10
    at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:1154:10)
    at Mongoose.connect (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:349:20)
    at Object.<anonymous> (C:\COURSES\Authentication-nodeJS\index.js:13:4)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4364) 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(). 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: 2)
(node:4364) [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.

现在我收到了这个奇怪的错误。但它显示数据库已连接。任何人都可以帮助我

提前谢谢你

【问题讨论】:

  • 我已经遵循了所有这些步骤,但找不到确切的解决方案。
  • 确保您以正确的方式阅读配置,只需在 connect 函数之前放置一个 console.log() 并检查 process.env.DB_CONNECT 的值,而且您不需要这一行 @ 987654327@
  • 你看错了吗?

标签: javascript node.js mongodb


【解决方案1】:

server up and Running Successfully and DB Connected
(node:11416) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
    at MessageStream.messageHandler (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
    at MessageStream._write (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at TLSSocket.ondata (internal/streams/readable.js:719:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11416) 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(). 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:11416) [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.

强调文字您可以按照以下步骤操作,我认为您的代码在此之后应该可以工作

  1. 用您在 MongoDB 站点上的帐户的实际用户名和密码替换下面变量中的这个
://username:<password>
DB_CONNECT = 'mongodb+srv://username:<password>@cluster0.5tnd3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'

to something like this

MONGO_URL_TEST= "mongodb+srv://myname:myapssword@cluster0.z4ta5.mongodb.net/test?retryWrites=true&w=majority"
  1. 使用 Mongoose 提供的选项解决这些弃用警告
const options = {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  useUnifiedTopology: true,
};
mongoose.connect(MONGO_URL, options);
  1. 不需要这个
const dotenv = require("dotenv");

below is enough

require('dotenv').config();

4.确保您的 .env 文件位于文件的根目录中,而不是在任何文件夹中

在这里阅读更多:

https://mongoosejs.com/docs/connections.html

https://mongoosejs.com/docs/deprecations.html

服务器正在运行,但仍收到这些错误和警告。我真的应该担心这个吗?因为如果我验证,我会在邮递员中得到空数据

【讨论】:

  • @Siddarth A 你收到什么样的错误信息?
  • Hey Bro 服务器正在运行,DBis 也已连接。但我同时收到上述错误和警告。我什至将 mongoose 版本从新版本更改为旧版本。但我仍然不能认识。我被卡住了
  • 感谢它现在可以使用旧版本的 mongoose 包。
猜你喜欢
  • 2019-08-27
  • 2021-06-11
  • 1970-01-01
  • 2021-05-21
  • 2020-03-06
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多