为什么我得到"不是一个功能"错误?

时间:2018-02-16 13:19:14

标签: javascript node.js express ecmascript-6

我想为服务器应用程序编写测试类。我需要将我的路由器重构为控制器拱。 我在控制器的返回值上返回函数。但在我的路由器上我看到错误。你有什么主意吗?我通过哪一点?

控制器:

const express = require('express');

const controller = (Project) => {
    const projectMidlleware = require('../middleware/v1/projectMiddleware')(Project)
    const put = (req, res, next) => {
        projectMidlleware.create(req.query.name, (project) => {
            res.status(200).send(project.api_key);
            next();
        });
    }
    const findProject = (req, res, next) => {
        const apikey = req.headers["s-apikey"] || req.query.apikey || req.body.apikey;
        projectMidlleware.findByApikey(apikey, (err, project) => {
            if (err) {
                next({
                    message: err.message,
                    code: 500
                });
                return;
            }
            req.body.project = project;
            next();
        });
    }

    return {
        put,
        findProject
    }
}
module.exports = controller;

路由器:

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

const projectController = require('../../controllers/projectController');
const profileController = require('../../controllers/profileController');

//return profiles for project
router.get("/",
  projectController.findProject(),
  profileController.get()
);

module.exports = router;

1 个答案:

答案 0 :(得分:0)

REZULTAT

你有一个函数,它返回一个带有方法的对象,但是你没有调用该函数,并且返回的对象上存在方法,而不是函数本身。这种模式在节点中相当常见,但为了清楚起见,我实际上是这样做的:

const projectController = require('../../controllers/projectController')();

然后使用它。