当我发送DELETE请求时,出现消息错误
ReferenceError: id is not defined
at Object.removeOne (...\services\user.js:16:38
我不知道\ services \ user.js中的ID是什么,为什么不定义它...
./ generalRepository.js
function Repository() {}
Repository.prototype.findAndRemoveById = findAndRemoveById;
function findAndRemoveById(id, callback) {
var model = this.model;
var query = model.deleteOne({
_id: id
});
query.exec(callback);
}
module.exports = Repository;
.routers / user.js
const router = require("express").Router();
const userService = require("../../services/user");
router.delete("/:id", (req, res, next) => {
userService.removeOne(String(req.params.id), (err, data) => {
if (!err) {
res.send('success delete query');
} else {
console.log("wrong delete query");
res.status(400);
res.end();
}
});
});
module.exports = router;
.services / user.js
const UserRepository = require("../repositories/UserRepository");
module.exports = {
removeOne: () => {
UserRepository.findAndRemoveById(id, (err, data) => {
callback(err, data);
});
}
};
答案 0 :(得分:0)
您需要将removeOne
函数更新为跟随函数,以期望在调用它时传递参数(id
和callback
)。
const UserRepository = require("../repositories/UserRepository");
module.exports = {
removeOne: (id, callback) => {
UserRepository.findAndRemoveById(id, (err, data) => {
callback(err, data);
});
}
};
答案 1 :(得分:0)
也许你应该是这样的:
module.exports = {
removeOne: (id) => {
UserRepository.findAndRemoveById(id, (err, data) => {
callback(err, data);
});
}
};