【发布时间】:2019-11-05 19:00:57
【问题描述】:
我正在尝试在 mongodb 上进行一些 crud 操作,
当我摆脱异步等待时,我得到了 200 代码,但没有任何东西保存在 db 上,我猜这是正常的,当我在邮递员上测试我的路由时,使用异步代码我得到“服务器无响应”错误。
/routes.js
const router = require('express-promise-router')();
const appointmentController =require ('../controllers/appointments');
router.get('/list',appointmentController.list);
router.post('/add',appointmentController.add);
module.exports = router
/controllers.js
const Appointment = require("../models/Appointment");
module.exports = {
list: async (req, res) => {
const appointment = await Appointment.find();
res.send({appointment});
},
add: async (req, res) => {
const appointment = new Appointment(req.body);
await appointment.save();
return res.status(200).json('saved to the db !');
},
};
/模型:
const appointmentSchema = new Schema({
Neurologist: {type: String, required: true},
Remarks: String,
Date: { type: Date, required: true },
Hour: { type: Number, required: true },
Type: {type: String, required: true}
});
const Appointment = mongoose.model("appointment", appointmentSchema);
我看不出我做错了什么,我还是个新手,任何帮助将不胜感激!
【问题讨论】:
-
您可以添加模型以便我们提供帮助吗?
-
@zardilior 我的错,我刚刚更正了文件名:-)