"在user.controller中我将此方法"放在中
exports.delete= (req, res) => {
User.findByIdAndRemove(req.params.id)
.then((user) => {
if (!user) {
return res.status(404).send({
message: "User not found ",
});
}
res.send({ message: "User deleted successfully!" });
})
.catch((err) => {
return res.status(500).send({
message: "Could not delete user ",
});
});
};
}
",在user.routes中,我甚至在使用put或获取此错误时也会将此代码显示为">
const controller = require("../controllers/user.controller");
module.exports = function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
app.delete("/api/users/Delete/:id", controller.delete);
}
由于controller.delete
是undefined
,模块内的require()
语句或exports
显然存在问题,因为.delete
方法不在您期望的模块上。由于您没有显示所有导入和导出的相关代码,因此我们无法提供更具体的帮助。