简体   繁体   中英

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. 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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM