猫鼬和 MERN:无法调用和路由多个发现
Mongoose and MERN: unable to call and route multiple finds
我正在使用 react-big-calendar,我想要一个 findById(操作每个事件以进行编辑和删除)和一个 findByUser(用于持久化数据库)。
控制器
const db = require("../models");
module.exports = {
findAll: function(req, res) {
db.Event
.find()
.then(dbModel => {
res.json(dbModel)
})
.catch(err => res.status(422).json(err));
},
findByUser: function(req, res) {
db.Event
.find({user: req.params.user})
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
findById: function(req, res) {
db.Event
.findById(req.params.id)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
create: function(req, res) {
db.Event
.create(req.body)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
update: function(req, res) {
db.Event
.findOneAndUpdate({ "_id": req.params.id },
{
"title": req.body.title,
"start": req.body.start,
"end": req.body.end,
"description": req.body.description
},
{ new: true }
)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err))
},
remove: function(req, res) {
db.Event
.findById({ _id: req.params.id })
.then(dbModel => dbModel.remove())
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
}
}
路线
const router = require("express").Router();
const calendarController = require("../../controllers/calendarController");
const passport = require("passport");
router.route("/")
.get(calendarController.findAll)
.post(calendarController.create);
router.route("/:user")
.get(calendarController.findByUser);
router.route("/:id")
.get(calendarController.findById)
.put(calendarController.update)
.delete(calendarController.remove);
module.exports = router;
此时,findById 返回一个空数组。如果我交换用户路由和 id 路由的顺序,它 findById 然后工作,但用户然后 returns 为空。这里发生了什么事?我可以通过ID和用户ID分别调用一个文件吗?
实际上这里你的路由 /:user
和 /:id
没有什么不同......谁先找到,先执行,其他的将被覆盖
摆脱这种情况的唯一方法是更改路由的名称
router.route("/user/:user") // change the route name here
.get(calendarController.findByUser);
router.route("/:id")
.get(calendarController.findById)
.put(calendarController.update)
.delete(calendarController.remove);
我正在使用 react-big-calendar,我想要一个 findById(操作每个事件以进行编辑和删除)和一个 findByUser(用于持久化数据库)。
控制器
const db = require("../models");
module.exports = {
findAll: function(req, res) {
db.Event
.find()
.then(dbModel => {
res.json(dbModel)
})
.catch(err => res.status(422).json(err));
},
findByUser: function(req, res) {
db.Event
.find({user: req.params.user})
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
findById: function(req, res) {
db.Event
.findById(req.params.id)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
create: function(req, res) {
db.Event
.create(req.body)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
update: function(req, res) {
db.Event
.findOneAndUpdate({ "_id": req.params.id },
{
"title": req.body.title,
"start": req.body.start,
"end": req.body.end,
"description": req.body.description
},
{ new: true }
)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err))
},
remove: function(req, res) {
db.Event
.findById({ _id: req.params.id })
.then(dbModel => dbModel.remove())
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
}
}
路线
const router = require("express").Router();
const calendarController = require("../../controllers/calendarController");
const passport = require("passport");
router.route("/")
.get(calendarController.findAll)
.post(calendarController.create);
router.route("/:user")
.get(calendarController.findByUser);
router.route("/:id")
.get(calendarController.findById)
.put(calendarController.update)
.delete(calendarController.remove);
module.exports = router;
此时,findById 返回一个空数组。如果我交换用户路由和 id 路由的顺序,它 findById 然后工作,但用户然后 returns 为空。这里发生了什么事?我可以通过ID和用户ID分别调用一个文件吗?
实际上这里你的路由 /:user
和 /:id
没有什么不同......谁先找到,先执行,其他的将被覆盖
摆脱这种情况的唯一方法是更改路由的名称
router.route("/user/:user") // change the route name here
.get(calendarController.findByUser);
router.route("/:id")
.get(calendarController.findById)
.put(calendarController.update)
.delete(calendarController.remove);