从前端表单发出发布请求时出现内部服务器错误500

问题描述

我通过在前端填写表格来发出发布请求时,收到内部服务器错误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,gender: {
        type: String,email: {
        type: String,branch: {
        type: String,nationality: {
        type: String,required: true,},address:{
        type: String,fatherName: {
        type: String,motherName: {
        type: String,fatherMobile: {
        type: String,dob: {
        type: String,photo: {
        type: String,roomNo: {
        type: String,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,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.verifyAdmin,next) => {
    res.end('Put request not valid on the /students end point')
})

.post(cors.corsWithOptions,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); })
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)