为什么没有定义ID?

时间:2018-07-12 09:31:01

标签: javascript node.js express mongoose

当我发送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);
    });
  }
};

2 个答案:

答案 0 :(得分:0)

您需要将removeOne函数更新为跟随函数,以期望在调用它时传递参数(idcallback)。

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);
    });
  }
};