无法获得单一类别-NodeJs API

时间:2019-11-03 19:23:54

标签: javascript node.js express

我正在为MERN的电子商务应用程序开发API。我已经做了一些事情,现在我正试图获得单一类别。控制台上没有错误,我读了几次代码,但是邮递员不断抛出Cannot GET错误。如果有人可以告诉我该怎么处理,我将不胜感激。 创建新类别的部分工作正常,也与获得一个产品代码的类似代码一样: Category.js路由器

const express = require("express");
const router = express.Router();

const { create, categoryById, get } = require("../controllers/category");
const { requireSignin, isAuth, isAdmin } = require("../controllers/auth");
const { userById } = require("../controllers/user");

router.get("/category/:categoryId", get);
router.post("/category/create/:userId", requireSignin, isAuth, isAdmin, create);

router.param("categoryId", categoryById);
router.param("userId", userById);

Category.js控制器

const Category = require("../models/category");
const { errorHandler } = require("../helpers/dbErrorHandler");

exports.categoryById = (req, res, next, id) => {
  Category.findById(id).exec((err, category) => {
    if(err || !category) {
      return res.status(400).json({
        error: 'Category does not exist'
      });
    }
    req.category = category;
    next();
  });
}

exports.create = (req, res) => {
  const category = new Category(req.body);
  category.save((err, data) => {
    if (err) {
      return res.status(400).json({
        error: errorHandler(err)
      });
    }
    res.json({ data });
  });
};

exports.get = (req, res) => {
  return res.json(req.category);
}

0 个答案:

没有答案