同时,数据没有保存在数据库中……当我点击发给邮递员的邮递请求时,他给了我html页面而不是json。邮递员中的所有设置均已正确配置。
classesModel.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const classes = new Schema({
classStatus: {
type: Boolean,
default: false,
},
studentName: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
status: {
type: String,
required: [true, 'Please select your status'],
},
reason: String,
});
const Class = mongoose.model('Class', classes);
module.exports = Class;
classROuter.js
const express = require('express');
const router = express.Router();
const { class_get, class_post } = require('../controllers/classController');
/* Routes */
router.get('/', class_get);
router.post('/', class_post);
module.exports = router;
classController.js
const Class = require('../models/classesModel');
const class_get = (req, res) => {
res.render('pages/class/class');
// res.send('class page');
};
const class_post = async (req, res) => {
const { status, reason } = req.body;
try {
await Class.create({ status, reason });
res.status(201).json({
status: 'successful',
});
} catch (err) {
if (err) console.log(err); //!
}
};
module.exports = {
class_get,
class_post,
};
app.js
app.use('*', checkUser);
app.use('/', homeRoutes);
app.use('/class', requireAuthentication, classRoutes);
app.use('/records', requireAuthentication, recordsRoutes);
app.use('/media', requireAuthentication, mediaRoutes);