我通过在前端填写表格来发出发布请求时,收到内部服务器错误500的信息,请有人帮忙。另外,当我向邮递员发出请求时,也会发生相同的错误。该如何解决?
学生模型后端代码和前端代码
const mongoose = require('mongoose');
const Schema = mongoose.Schema
const studentSchema = new Schema({
studentName: {
type: String,
required: true
},
sid: {
type: String,
unique: true,
required: true
},
mobileNo: {
type: String,
unique: true,
required: true
},
gender: {
type: String,
required: true
},
email: {
type: String,
required: true
},
branch: {
type: String,
required: true
},
nationality: {
type: String,
required: true,
},
address:{
type: String,
required: true,
},
fatherName: {
type: String,
required: true
},
motherName: {
type: String,
required: true
},
fatherMobile: {
type: String,
required: true
},
dob: {
type: String,
required: true
},
photo: {
type: String,
required: true
},
roomNo: {
type: String,
required: true
},
hostel: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Hostel'
},
});
const Students = mongoose.model("Student", studentSchema);
module.exports = Students
后端代码
studentRouter.route('/')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200); })
.get(cors.cors, authenticate.verifyUser, (req, res, next) => {
console.log(req.user.hostel)
Students.find({hostel: req.user.hostel})
.populate('hostel')
.then((students) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json')
res.json(students);
}, err => next(err))
.catch(err => next(err))
})
.put(cors.corsWithOptions, authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => {
res.end('Put request not valid on the /students end point')
})
.post(cors.corsWithOptions, authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => {
req.body.hostel = req.user.hostel;
Students.create(req.body)
.then((student) => {
Students.findById(student._id)
.populate('hostel')
.then((student) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(student)
}, err => next(err))
}, (err) => next(err))
.catch((err) => next(err))
})
前端代码
export const postStudent = (student) => (dispatch) => {
const newStudent = {
studentName: student.name,
sid: student.id,
mobileNo: student.mobile,
gender: student.gender,
email: student.email,
branch: student.branch,
nationality: student.nationality,
address: student.address,
fatherName: student.father,
motherName: student.mother,
fatherMobile: student.Fnum,
dob: student.dob,
photo: student.photo
}
console.log('Student: ', newStudent);
const bearer = 'Bearer ' + localStorage.getItem('token');
return fetch(baseUrl + 'students', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': bearer
},
body: JSON.stringify(newStudent),
credentials: "same-origin"
})
.then(response => {
console.log(response);
if (response.ok) {
return response;
}
else {
var error = new Error('Error ' + response.status + ': ' + response.statusText);
error.response = response;
throw error;
}
},
error => {
var errmess = new Error(error.message);
throw errmess;
})
.then(response => response.json())
.then(response => dispatch(addStudent(response)))
.catch(error => { console.log('Post students ', error.message);
alert('Your student could not be added\nError: '+ error.message); })
}