最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Route.get()需要回调函数但得到一个[对象Promise]

IT培训 admin 6浏览 0评论

Route.get()需要回调函数但得到一个[对象Promise]

我正在创建一个带有express的REST API,从this article开始构建体系结构。简而言之,路由器正在调用控制器。

这是一个电话的例子:

router.get('/', ModelsController.getModels)

到目前为止这项工作正常,现在,我正在改进Boom的错误处理。

我想在this article中使用包装器,但由于我不使用TS,因为我不熟悉Promises,所以我正在努力解决它。

这是包装器:

exports.enhanceHandler = async function (handler) {
    return async function (req, res, next) {

        try {
            const result = await handler(req, res);
            if (result instanceof Error && Boom.isBoom(result)) {
                res.status(result.output.statusCode).send(formatBoomPayload(result));
            }
        } catch (error) {
            // now log errors to your errors reporting software
            if (process.env.NODE_ENV !== "production" && (error.stack || error.message)) {
                res.status(500).send(error.stack || error.message);
            } else {
                res.status(500).send(Boom.internal().output.payload);
            }
        }

        next();
    }
}

我试图在我的路由器中调用它,如下所示:

router.get('/handler', enhanceHandler(ModelsController.getModels))

但是,我有这个错误:

Error: Route.get() requires a callback function but got a [object Promise]

我能做什么 ?我需要解决这个承诺吗?修改enhanceHandler所以它返回一个函数而不是一个promise?

回答如下:

每个promise对象都有一个.then方法,您需要使用该方法从promise对象中获取结果,如下所示:

handler(req, res).then((res) => {
    if (res instanceof Error && Boom.isBoom(res)) {
        res.status(res.output.statusCode).send(formatBoomPayload(res));
    }
});

如果我们不再使用等待,我们也可以从函数中删除异步。

Route.get()需要回调函数但得到一个[对象Promise]

我正在创建一个带有express的REST API,从this article开始构建体系结构。简而言之,路由器正在调用控制器。

这是一个电话的例子:

router.get('/', ModelsController.getModels)

到目前为止这项工作正常,现在,我正在改进Boom的错误处理。

我想在this article中使用包装器,但由于我不使用TS,因为我不熟悉Promises,所以我正在努力解决它。

这是包装器:

exports.enhanceHandler = async function (handler) {
    return async function (req, res, next) {

        try {
            const result = await handler(req, res);
            if (result instanceof Error && Boom.isBoom(result)) {
                res.status(result.output.statusCode).send(formatBoomPayload(result));
            }
        } catch (error) {
            // now log errors to your errors reporting software
            if (process.env.NODE_ENV !== "production" && (error.stack || error.message)) {
                res.status(500).send(error.stack || error.message);
            } else {
                res.status(500).send(Boom.internal().output.payload);
            }
        }

        next();
    }
}

我试图在我的路由器中调用它,如下所示:

router.get('/handler', enhanceHandler(ModelsController.getModels))

但是,我有这个错误:

Error: Route.get() requires a callback function but got a [object Promise]

我能做什么 ?我需要解决这个承诺吗?修改enhanceHandler所以它返回一个函数而不是一个promise?

回答如下:

每个promise对象都有一个.then方法,您需要使用该方法从promise对象中获取结果,如下所示:

handler(req, res).then((res) => {
    if (res instanceof Error && Boom.isBoom(res)) {
        res.status(res.output.statusCode).send(formatBoomPayload(res));
    }
});

如果我们不再使用等待,我们也可以从函数中删除异步。

发布评论

评论列表 (0)

  1. 暂无评论