首页 > 解决方案 > 当我将我的快速代码从路由文件移动到控制器文件时,api 不起作用。从邮递员那里点击 api 会导致一个无休止的请求

问题描述

当我在 authorRouter.js 文件中编写帖子和获取函数时,api 正在工作,但是当我将这些函数移动到控制器文件夹中的 authorController.js 文件时,api 停止工作。当我从邮递员那里调用 api 时,它会导致一个永无止境的请求。我之前是这样从路由器文件中调用控制器函数的:

.post(controller.post)
.get(controller.get)

但它给了我一个错误“Route.get() 需要一个回调函数,但得到了一个 [object Undefined]”,所以我编辑了我的路由器文件,如下所示:

authorRouter.js 文件

const express = require('express');
const authorController = require('../controllers/authorController');

function routes(Author){
    const authorRouter = express.Router();
    const controller = authorController(Author);
    authorRouter.route('/author')
    .post( function(req, res){
        controller.post
      })
      .get( function(req,res){
           controller.get
       }     
       );
// .post((req,res)=>{
//     const author = new Author(req.body);

//     author.save();
//     return res.status(201).json(author);
// })
// .get((req,res)=>{
//     const query = {};
//     if(req.query.name){
//         query.name = req.query.name;
//     }
//      Author.find(query,(err,authors)=>{
//      if(err){
//          return res.send(err);
//      }
//         return res.json(authors);  
//     });
//  });

authorRouter.use('/author/:authorId',(req,res,next)=>{
    Author.findById(req.params.authorId,(err,author)=>{
        if(err){
            return res.send(err);
        }
        if(author){
            req.author =author;
            return next();
        }
           return res.sendStatus(404);  
       });
});
authorRouter.route('/author/:authorId')
.get((req,res)=>{
    res.json(req.author);
 })
 .put((req,res)=>{
         const {author}  =req;
         author.name = req.body.name;
         author.book = req.body.book;
         author.age = req.body.age;
         req.author.save((err)=>{
            if(err){
                return res.send(err);
            }
            return res.json(author);
        })

 })
 .patch((req,res)=>{
     const { author} =req;
     if (req.body._id){
         delete req.body._id;
     }
     Object.entries(req.body).forEach(item=>{
         const key = item[0];
         const value = item[1];
         author[key]= value;
     });
     req.author.save((err)=>{
         if(err){
             return res.send(err);
         }
         return res.json(author);
     });
 })
 .delete((req,res)=>{
     req.authorId.remove((err)=>{
         if(err){
            return res.send(err);
         }
         return res.sendStatus(204);
     })

 });

 return authorRouter;

}


module.exports= routes;

authorController.js 文件

 function authorController(Author){
    function post(req,res){
      const author = new Author(req.body);
      author.save();
      return res.status(201).json(author);
    }

    function get(req,res) {
      const query = {};
      if(req.query.name){
        query.name = req.query.name;
      }
      Author.find(query,(err,authors)=>{
       if(err){
         return res.send(err);
        }
        return res.json(authors);  
      });   
    }
        return(post, get );
  }

  module.exports = authorController;

为什么会这样?

标签: javascriptnode.jsexpress

解决方案


你能试试这个

.post(function (req, res) {
  controller.post(req, res);
})

或者

.post(controller.post);

推荐阅读