简体   繁体   中英

MongooseError [OverwriteModelError]: Cannot overwrite `Team` model once compiled

I'm making a call to a node.js express api from a react client.

When I make the call from the client, this is my request:

const response = await axios({
            method: 'post',
            url: 'http://localhost:3000/api/users/forgotPassword',
            data: {email: email},
            headers: {
              'X-Requested-With': 'XMLHttpRequest',
            }
          }
        );

This is the endpoint in express:

adminUserRoutes.post('/forgotPassword', (req, res) => {
  console.log('it connected')
  if (req.body.email === '') {
    res.status(400).send('email required');
  }
  console.log(req.body.email)
  console.log(req.body)
  User.findOne({email: req.body.email}, (err, user) => {
    console.log('and here')
    if(user){
      const token = crypto.randomBytes(20).toString('hex');
      console.log('use',user)
      user.resetPasswordToken = token
      user.resetPasswordExpires = Date.now() + 360000
      user.name = user.name
      user.email = user.email
      user.password = user.password
      user.admin = user.admin

      // console.log(user)

      user.save()


      const transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
          user: `email`,
          pass: `password`,
        },
      });

      const mailOptions = {
        from: 'devinjjordan@gmail.com',
        to: `${user.email}`,
        subject: 'Link To Reset Password',
        text:
          'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n'
          + 'Please click on the following link, or paste this into your browser to complete the process within one hour of receiving it:\n\n'
          + `http://localhost:3000/#/newpassword/${token}\n\n`
          + 'If you did not request this, please ignore this email and your password will remain unchanged.\n',
      };

      console.log('sending mail');

      transporter.sendMail(mailOptions, (err, response) => {
        if (err) {
          console.error('there was an error: ', err);
          // res.status(200).json('there was an error: ', err);
        } else {
          console.log('here is the res: ', response);

          res.set({

              "Access-Control-Allow-Origin": "*", // Required for CORS support to work
              "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS

          })

          res.status(200).json('recovery email sent');
        }
      });
    } else {
      console.error('email not in database');
      res.status(403).send('email not in db');
    }
  })
});

What's odd about the situation is that when I make the request from postman to the same endpoint, I receive the expected response.

However, when I make the request from the client, I receive this error:

MongooseError [OverwriteModelError]: Cannot overwrite `Team` model once compiled.
    at new OverwriteModelError (/Users/lukeschoenberger/Documents/Programming/news-arg/backend/node_modules/mongoose/lib/error/overwriteModel.js:20:11)
    at Mongoose.model (/Users/lukeschoenberger/Documents/Programming/news-arg/backend/node_modules/mongoose/lib/index.js:517:13)

I'm using serverless labdma and am running sls offline start to open on port 3000.

What's very odd is that the 'Team' model isn't even mentioned in the api in question.

Edit: This is the Team module:

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

let Team = new Schema({
    team_name: {
        type: String
    },
    city_or_state: {
        type: String
    },
    league: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'League'
    },
    primary_color: {
        type: String
    }
}, { timestamps: true })

module.exports = mongoose.model('Team', Team)

This turned out to be an issue with withe aws-serverless. Running aws-serverless with this flag solves the issuue: --skipCacheInvalidation -c . Longer post about it: https://github.com/dherault/serverless-offline/issues/258

I also ran in to same error few weeks ago. After trying out few things I came out with a simple fix:

just try to export person model this way -

module.exports.teamModel= mongoose.model('Team', Team);

instead of - module.exports = mongoose.model('Team', Team)

Hope this helps !

if you still get the error just check the paths in the modules you were exporting this model.

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