NodeJS 未解析的函数或方法connect()mongoose

jv4diomz  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(249)

我正在学习Mongoose从JavaScript无处不在的书,这是我必须写的代码:

const mongoose = require('mongoose');
const mongoose = require('mongoose');
module.exports = {
  connect: DB_HOST => {
    mongoose.connect(DB_HOST);
    mongoose.connection.on('error', err => {
      console.error(err);
      console.log('MongoDB connection error. Please make sure MongoDB is running.');
      process.exit();
    });
  },
  close: () => {
    mongoose.connection.close();
  }
}

当我把鼠标悬停在connectconnection上时,它会相应地显示Unresolved function or method connect()Unresolved variable connection。我猜这本书是关于mongoose的旧版本,而在最新版本中它只是被删除了。它的新函数和变量是什么?

1tu0hz3e

1tu0hz3e1#

我也在学 Mongoose ,情况和你一样;下面是我如何修复错误-您可以将其用作修复代码的参考:

const {connect: connect1} = require('mongoose')

async function connect() {
    try {
        await connect1('DB_HOST')

        console.log('\n Connected successfully')
    } catch (err) {
        console.log('\n Disconnect')

    }
}

module.exports = { connect }

相关问题