mongodb - Mongoose 模块中 collection.js 中未定义的 Mongoose TypeError 'cannot read namespace'

标签 mongodb mongoose

当我使用 NPM 运行我的应用程序时,它运行没有问题。当使用我的 VSC 调试器运行它时,我在位于 node_modules/mongodb/lib/collection.js 的 collection.js 中两次收到以下错误:

TypeError: Cannot read property 'namespace' of undefined
    at Object.get [as dbName] (/xxxx/xxxx/xxxx/xxxx/node_modules/mongodb/lib/collection.js:184:19)
    at Object.<anonymous> (/xxxx/xxxx/xxxx/xxxx/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:184:36)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/xxxx/xxxx/xxxx/xxxx/node_modules/mongoose/lib/drivers/node-mongodb-native/index.js:8:22)

此处选取实例 1:

Object.defineProperty(Collection.prototype, 'dbName', {
  enumerable: true,
  get: function() {
    return this.s.namespace.db;
  }
});

此处选取实例 2:

Object.defineProperty(Collection.prototype, 'collectionName', {
  enumerable: true,
  get: function() {
    return this.s.namespace.collection;
  }
});

我位于 app.js 中的连接配置如下所示(使用 NPM 运行应用程序时可以正常工作,没有任何错误):

  const dbUser = process.env.xxxxx;
  const dbPass = process.env.xxxxx;
  const dbURL = process.env.xxxxx;
  const dbName = process.env.xxxxxx;

mongoose.set('useCreateIndex', true);
mongoose.connect(`mongodb+srv://${dbUser}:${dbPass}@${dbURL}/${dbName}`, {useNewUrlParser: true , useUnifiedTopology: true} )
  .then(_ => console.log('MongoDB successfully connected!'))
  .catch(err => console.error(err));

此应用程序中唯一加载 Mongoose 的 Mongoose 模型如下:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// UserSchema has been omitted for privacy
const UserSchema = new Schema({
});

module.exports = User = mongoose.model('User', UserSchema);

没有其他实例加载 Mongoose 模块。我还检查了在 mongoose.connect 方法中传递的选项,但似乎也不是它们。此外,我检查了 env 文件中存储的值,它们似乎也没有导致问题。

谢谢,非常感谢任何帮助!

最佳答案

更新nodemon、mongoose

npm i -g nodemon
npm i mongoose

如果您收到包依赖项的警告,例如:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules\chokidar\node_modules\fsevents):
=> npm i fsevents

也不要安装可选的依赖项。

关于mongodb - Mongoose 模块中 collection.js 中未定义的 Mongoose TypeError 'cannot read namespace',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59120834/

相关文章:

mongodb - 在 MongoDB 中,你如何查询只包含某些字段而不包含其他字段的记录

php - 在 Mac OS Lion 上使用 XAMPP 安装 MongoDb PHP 驱动程序时出错

javascript - 从返回的 MongoDB 对象更新 Angular.js $scoped

javascript - 返回资源对象而不是 Mongoose.count 值

javascript - Node.js/Mongoose 类型错误

mongodb - 如何从超过一定限制的单个集合中删除文档

java - 如何使用spring data获取mongodb中最大聚合函数对应的值

javascript - 使用 NodeJS 获取 API 数据并写入 MongoDB

javascript - 所有 Mongoose 模型方法都是基于 promise 的吗?

javascript - 为什么 postman 中的响应有效,但我的测试无效?