简体   繁体   English

错误:无法读取未定义的属性(读取“主机”)

[英]Error: Cannot read properties of undefined (reading 'host')

I am trying to connect my application to MongoDB, but getting this error 'Error: Cannot read properties of undefined (reading 'host')'.我正在尝试将我的应用程序连接到 MongoDB,但收到此错误“错误:无法读取未定义的属性(读取“主机”)”。 Not able to figure out what might be the problem.无法弄清楚可能是什么问题。

Here is the.env file这是.env文件

PORT=5000;
MONGO_URI ='mongodb+srv://sakshi_0630:<password>@cluster0.lxchu.mongodb.net/?retryWrites=true&w=majority'

db.js file db.js 文件

const mongoose = require("mongoose");
const colors = require("colors");
const connectDB=async()=>{
    try {
        const conn = mongoose.connect(process.env.MONGO_URI, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: true,
        });
        console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline);
    } catch (error) {
        console.log(`Error: ${error.message}`.red.bold);
        process.exit();
    }
};
module.exports= connectDB;

server.js服务器.js

const { response } = require("express");
const express = require("express");
const dotenv = require("dotenv");
const { chats } = require("./data/data");
const connectDB = require("./config/db");

dotenv.config();
connectDB();
const app = express();

You need to await the mongoose.connect() function您需要await mongoose.connect() function

const connectDB = async() => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: true,
        });
        console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline);
    } catch (error) {
        console.log(`Error: ${error.message}`.red.bold);
        process.exit();
    }
};

or use a callback function或使用回调 function

mongoose.connect(process.env.MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useFindAndModify: true,
})
.then((conn) => {
    console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline);
    //do other things
})
.catch((error) => {
    console.log(`Error: ${error.message}`.red.bold);
    process.exit();
});

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

相关问题 类型错误:无法读取未定义的属性(读取“主机”) - TypeError: Cannot read properties of undefined (reading 'host') 错误:无法读取未定义的属性(读取“get”) - Error: Cannot read properties of undefined (reading 'get') 错误无法读取未定义的属性(读取“indexOf”) - error cannot read properties of undefined (reading 'indexOf') 错误:无法读取未定义的属性(读取“管理员”) - Error: Cannot read properties of undefined (reading 'admin') 错误无法读取未定义的属性(读取“配置”)。 TypeError:无法读取未定义的属性(读取“配置”) - ERROR Cannot read properties of undefined (reading 'configurations'). TypeError: Cannot read properties of undefined (reading 'configurations') TypeError:无法读取未定义(读取“过滤器”)和未定义错误的属性 - React - TypeError: Cannot read properties of undefined (reading 'filter') and undefined error - React 未定义错误:TypeError:无法读取未定义的属性(读取“图标”) - Undefined Error: TypeError: Cannot read properties of undefined (reading 'Icon') 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4') 无法读取未定义的属性(读取“______”) - Cannot read properties of undefined (reading '______')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM