[英]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获取未正常提供的数据,例如
accessToken
( session.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.