简体   繁体   English

Mongoose: OverwriteModelError: 无法覆盖`users` model 一旦编译

[英]Mongoose: OverwriteModelError: Cannot overwrite `users` model once compiled

I'm using Mongoose to manage a MongoDB server, and all other solutions with this error have not helped.我正在使用 Mongoose 来管理 MongoDB 服务器,所有其他出现此错误的解决方案都没有帮助。 The models aren't defined anywhere else, and the only issue I can think of is that the tables/models already exist on the MongoDB server, but other than that I've never had this issue before.模型没有在其他任何地方定义,我能想到的唯一问题是表/模型已经存在于 MongoDB 服务器上,但除此之外,我以前从未遇到过这个问题。

My current code is this:我目前的代码是这样的:

const mongoose = require('mongoose');
mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true, useUnifiedTopology:true});

const users = require('../../models/Users.schema')
const accounts = require('../../models/Account.schema')

export default (req, res) => {
    users.findOne({ email: req.body.email }, function (err, user) {
        console.log(user)
        accounts.findOne({ userId: user._id }, function (err, account) {
            console.log(account)
          return res.json({
            error: false,
            body: account
          })
        })
    })
}

Users schema (mainly for an example, this issue happens with all schemas)用户架构(主要作为示例,所有架构都会出现此问题)

const mongoose = require('mongoose');

const usersSchema = new mongoose.Schema({
    name: String,
    email: String,
    image: String,
    createdAt: Date,
    updatedAt: Date
})

module.exports.users = mongoose.model('users', usersSchema);

What I'm trying to do is get the data from NextAuth that isn't provided normally, such as the accessToken ( session.accessToken is not the right accessToken).我要做的是从NextAuth获取未正常提供的数据,例如accessTokensession.accessToken不是正确的 accessToken )。

I'm not sure what to do, and I'll take any help I can get.我不知道该怎么做,我会尽我所能获得帮助。 Thanks!谢谢!

The error is occurring because you already have a schema defined, and then you are defining the schema again发生错误是因为您已经定义了架构,然后您再次定义架构

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

相关问题 Mongoose OverwriteModelError:编译后无法覆盖模型 - Mongoose OverwriteModelError: Cannot overwrite model once compiled Express,猫鼬:OverwriteModelError:编译后无法覆盖“ xxxxx”模型 - Express, Mongoose : OverwriteModelError : Cannot Overwrite 'xxxxx' model once compiled OverwriteModelError:编译后无法覆盖“团队”模型 - OverwriteModelError: Cannot overwrite `teams` model once compiled OverwriteModelError:编译后无法覆盖`actors`模型 - OverwriteModelError: Cannot overwrite `actors` model once compiled OverwriteModelError: 编译后无法覆盖`user` model - OverwriteModelError: Cannot overwrite `user` model once compiled OverwriteModelError:编译后无法覆盖`Product`模型 - OverwriteModelError: Cannot overwrite `Product` model once compiled OverwriteModelError:编译后无法覆盖 `User` model。 在 Mongoose.model - OverwriteModelError: Cannot overwrite `User` model once compiled. at Mongoose.model MongooseError [OverwriteModelError]:编译后无法覆盖`Team` model - MongooseError [OverwriteModelError]: Cannot overwrite `Team` model once compiled 编译后无法覆盖 model Mongoose - Cannot overwrite model once compiled Mongoose 如何修复:MongooseError [OverwriteModelError]:编译后无法覆盖 `User` 模型。(MERN 堆栈)? - How to fix:MongooseError [OverwriteModelError]: Cannot overwrite `User` model once compiled.(MERN stack)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM