MongooseError [OverwriteModelError]:编译后无法覆盖`Team`模型

我正在从React客户端调用node.js Express API。

当我从客户拨打电话时,这是我的要求:

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

这是快递的终点:

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,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');
    }
  })
});

这种情况的奇怪之处在于,当我从邮递员向同一端点发出请求时,我收到了预期的响应。

但是,当我从客户端发出请求时,会收到此错误:

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)

我正在使用无服务器实验室,并且正在脱机运行sls,以在端口3000上打开。

奇怪的是,相关api中甚至都没有提到“团队”模型。

编辑: 这是团队模块:

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)
eventuallyfantasy 回答:MongooseError [OverwriteModelError]:编译后无法覆盖`Team`模型

几周前,我也遇到了同样的错误。在尝试了几件事之后,我提出了一个简单的解决方法:

只需尝试以这种方式导出人物模型-

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

代替-module.exports = mongoose.model('Team',Team)

希望这会有所帮助!

如果仍然出现错误,请检查要导出此模型的模块中的路径。

,

原来,这是aws-serverless的问题。使用此标志运行aws-serverless可解决以下问题:--skipCacheInvalidation -c。更长的帖子:https://github.com/dherault/serverless-offline/issues/258

本文链接:https://www.f2er.com/3128269.html

大家都在问