【发布时间】: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