简体   繁体   English

Route.get() 需要回调函数但是得到了一个 [object Promise]

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

I am creating a REST API with express, folowing the architecture from this article .我正在创建一个带有 express 的 REST API,遵循本文中的架构。 in a nutshell, a router is calling a controller.简而言之,路由器正在调用控制器。

here is an example of a call:这是一个调用示例:

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

This work fine so far, and now, I'm improving error handling with Boom.到目前为止,这项工作很好,现在,我正在改进 Boom 的错误处理。

I would like to use the wrapper like in this article , but as I don't use TS and as I am unfamiliar with Promises, I'm struggling with it.我想使用的包装像这篇文章,但我不使用TS和我不熟悉的承诺,我与它挣扎。

Here is the wrapper:这是包装器:

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

I'm trying to call it in my router, like this:我试图在我的路由器中调用它,如下所示:

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

However, I've got this error:但是,我遇到了这个错误:

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

What could I do ?我能做什么 ? Do I need to resolve the promise ?我需要解决承诺吗? modify enhanceHandler so it return a function and not a promise ?修改 EnhanceHandler 使其返回一个函数而不是一个承诺?

Every promise object have a .then method that you need to use to get the result out of a promise object, like this: 每个promise对象都有一个.then方法,您需要使用.then方法来从promise对象中获取结果,如下所示:

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

We can also removes async from the function if we are not using await anymore. 如果我们不再使用等待,我们也可以从函数中删除异步。

Handler needs to be sync, but the function it returns can remain async. Handler 需要同步,但它返回的函数可以保持异步。

exports.enhanceHandler = function (handler) { // delete "async" keyword
   return async function (req, res, next) {

        try {
            const result = await handler(req, res);
            if (result instanceof Error && Boom.isBoo
    ..

Let's see what's going on. 让我们看看发生了什么。 You've called get() and for second parameter you've used enhanceHandler() call. 您已经调用了get(),并为第二个参数使用了EnhanceHandler()调用。 The call of any async function returns Promise . 任何异步函数的调用都会返回Promise While the get needs a function reference as the second parameter. 而get需要一个函数引用作为第二个参数。

So first you have to avoid async keyword on a function which provides the second parameter for get(). 因此,首先您必须避免在为get()提供第二个参数的函数上使用async关键字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点:Route.get() 需要一个回调函数,但得到了一个 [object Undefined] - Node: Route.get() requires a callback function but got a [object Undefined] nodejs Route.get()需要回调函数,但得到一个[object String] - nodejs Route.get() requires callback functions but got a [object String] 错误:Route.get()需要回调函数,但得到了一个[object Undefined] - Error: Route.get() requires callback functions but got a [object Undefined] Node JS:Route.get() 需要一个回调函数,但在使用 ES6 模块时得到了 [object Undefined] - Node JS : Route.get() requires a callback function but got a [object Undefined] While using ES6 Modules 错误:Route.get()需要回调函数,但得到了一个[object Undefined] NODE.JS + SQL - Error: Route.get() requires a callback function but got a [object Undefined] NODE.JS + SQL 如何解决:错误:Route.get() 需要回调 function 但得到了 [object Object]? - How to solve: Error: Route.get() requires a callback function but got a [object Object]? 错误:Route.get() 需要回调 function 但在使用导入的 function 时得到 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] while using imported function 错误:Route.get() 需要一个回调函数,但在 app.js 中得到一个 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] at app.js 节点服务器错误:Route.get() 需要回调 function 但得到了 [object Undefined] - node server Error: Route.get() requires a callback function but got a [object Undefined] “错误:Route.get() 需要回调 function 但得到 [object Undefined]” 进行多次导出时 - “Error: Route.get() requires a callback function but got a [object Undefined]” when doing multiple exporting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM