简体   繁体   English

Node.js 应用程序错误:MongooseError:`openUri()` 的 `uri` 参数必须是字符串,得到“未定义”

[英]Node.js App Error: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"

I have a Next.js app.我有一个 Next.js 应用程序。 I have an error with Mongodb. I couldn't solve this problem for a while.我的错误是Mongodb,一时没能解决这个问题。 That's why I wanted ask you guys.这就是为什么我想问问你们。 I hope we can solve it.我希望我们能解决它。 It's very important for me.这对我来说非常重要。 Firstly I'll give you my server.js codes, after that I'll give you the error message from console.首先,我会给你我的 server.js 代码,然后我会给你来自控制台的错误消息。

Server.js file codes: Server.js 文件代码:

const express = require('express')
const next = require('next')
const mongoose = require('mongoose')
require('dotenv/config')
const dev = process.env.NODE_ENV !== 'production'

const app = next({ dev })
const handle = app.getRequestHandler()
const port = process.env.SERVER_PORT || 3000

mongoose.connect(process.env.DATABASE,{
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
.then(() => {
  console.log('DB connected!')

  const port = process.env.PORT || 3000

  app.prepare().then(()=>{
    const server = express()
  
    //ROUTES
    server.use(express.json())
    server.get('*', (req, res) => handle(req, res))
  
    server.listen(port, (err) => {
      if(err) throw err
      console.log(`Server listen on http://127.0.0.1:${port}`)
    })
  
  })

})
.catch((err) => {
  console.log('DB ERROR:', err)
})

Error message on console:控制台上的错误消息:

DB ERROR: 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 (/home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/connection.js:684:11)
    at /home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/index.js:332:10
    at /home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/index.js:1158:10)
    at Mongoose.connect (/home/ubuntu/nodeApps/mywebsite-next/node_modules/mongoose/lib/index.js:331:20)
    at Object.<anonymous> (/home/ubuntu/nodeApps/mywebsite-next/server.js:11:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)

I have the link of Mongodb in my env file.我的 env 文件中有 Mongodb 的链接。 Maybe the error causes because of this situation?也许错误是由于这种情况引起的?

DATABASE=mongodb+srv://myusername:mypassword@cluster0.u4slu.mongodb.net/mydbusername?retryWrites=true&w=majority

You should put your login credentials to your mongodb url code like this:您应该将您的登录凭据放入您的 mongodb url 代码中,如下所示:

DATABASE=mongodb+srv://admin:admin@cluster0.u4slu.mongodb.net/<dbusername>?retryWrites=true&w=majority

If you did already try to give the path to dotenv:如果您已经尝试给出 dotenv 的路径:

require('dotenv').config({ path: 'ENV_FILENAME' });
mongoose.connect(`${process.env.DATABASE}`, () => {
  console.log('database connected successfully');
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongooseError:`openUri()` 的`uri` 参数必须是字符串,得到“未定义” - MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined" MongooseError:`openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。 `mongoose.connect()`ion()` 的第一个参数是一个字符串 - MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". first parameter to `mongoose.connect()`ion()` is a string `openUri()` 的 `uri` 参数必须是一个字符串,得到“undefined” - The `uri` parameter to `openUri()` must be a string, got "undefined" 错误:`openUri()` 的`uri` 参数必须是字符串,得到“未定义” - Error : The `uri` parameter to `openUri()` must be a string, got “undefined” MongooseError:`openUri()` 的`uri` 参数必须是字符串 - MongooseError: The `uri` parameter to `openUri()` must be a string "MongooseError('openUri()` 的 `uri` 参数必须是 '+" 即使变量是字符串也会出现错误 - "MongooseError('The `uri` parameter to `openUri()` must be a ' +" error appears even though variable is a string `openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。 确保 `mongoose.connect()` 或 `mongoose 的第一个参数 - The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose 连接 MongoDB 时出错:`openUri()` 的 `uri` 参数必须是字符串 - Getting an error while connecting MongoDB: The `uri` parameter to `openUri()` must be a string ZCCADCDEDB567ABAE643E15DCF0974E503Z 错误 'uri' 参数必须是未定义的字符串 - Mongoose Error 'uri' parameter must be a string got undefined Heroku 日志错误是 Mongoose 错误 uri 参数到 openURI() 必须是一个字符串,它是一个字符串 - Heroku logs error is Mongoose error uri parameter to openURI() must be a string and it is a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM