Urutan middlewares di sysaccess.js
Anda router salah.
Misalnya:
// "GET /sysaccess/test" will be processed by this middleware
router.get('/:id', (req, res) => {
let id = req.params.id; // id = "test"
Foo.findById(id).exec().then(() => {}); // This line will throw an error because "test" is not a valid "ObjectId"
});
router.get('/test', (req, res) => {
// ...
});
Solusi 1: jadikan middleware yang lebih spesifik itu sebelum yang lebih umum.
Misalnya:
router.get('/test', (req, res) => {
// ...
});
router.get('/:id', (req, res) => {
// ...
});
Solusi 2: gunakan next
untuk meneruskan permintaan ke middleware berikutnya
Misalnya:
router.get('/:id', (req, res, next) => {
let id = req.params.id;
if (id === 'test') { // This is "GET /sysaccess/test"
return next(); // Pass this request to the next matched middleware
}
// ...
});
router.get('/test', (req, res) => {
// ...
});