我正在从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)