【问题标题】:Return an API response from a Promise.then()从 Promise.then() 返回 API 响应
【发布时间】:2019-02-21 03:01:45
【问题描述】:

我正在编写一个 API 调用,它使用带有 express 和 bluebird 的 async Promise。 我有以下代码:

router.get('/', (req, res) => {
  log.debug('api - v2 - Koko Version api call');
  if (req.query.prop) {
    serverStatus.transform(req).then((data) => {
      switch (req.query.prop) {
        case 'KokoVersion': {
          return res.status(200).json({KokoVersion: data.version});
        }
        case 'KokoType': {
          return res.status(200).json({KokoType: data.deviceType});
        }
        case 'LastChangeTime': {
          return res.status(200).json({LastChangeTime: data.lastApply});
        }
      }
    }).catch((error) => {
      log.debug('Failed returning from Promise.resolve - v2 properties API');
    });
  }
  return res.status(500).json({status: 'ERROR', message: 'Internal Server error'});
});

transform 方法返回一个新的 Promise(),它生成上面看到的 then() 调用中使用的数据(在第 4 行),但是我似乎无法返回从上面的数据创建的字段的响应。所以我的问题是 - 当调用的评估需要通过 Promise 流程时,我如何响应 API 调用 --> 解决 --> 那么?

【问题讨论】:

  • 您是否尝试过从处理程序返回承诺?在serverStatus.transform... 行前添加return。而且您不需要返回res.status...
  • @SamiHult 我将从哪里发送对 API 调用的响应?
  • 它由您在res 对象上调用的方法发送。哦,记得end()回复:res.status(200)...end();
  • 对不起,我不明白。 “它是由您在 res 对象上调用的方法发送的”,这些方法是哪些?你指的是什么方法?
  • 当你写res.status(500).json(...).end()express时会发送输出。

标签: node.js rest express promise bluebird


【解决方案1】:
return res.status(500).json({status: 'ERROR', message: 'Internal Server error'});

此代码在执行之前返回 res,将其添加到代码中的 else 块

router.get('/', (req, res) => {
  log.debug('api - v2 - Koko Version api call');
  if (req.query.prop) {
    serverStatus.transform(req).then((data) => {
      switch (req.query.prop) {
        case 'KokoVersion': {
          return res.status(200).json({KokoVersion: data.version});
        }
        case 'KokoType': {
          return res.status(200).json({KokoType: data.deviceType});
        }
        case 'LastChangeTime': {
          return res.status(200).json({LastChangeTime: data.lastApply});
        }
      }
    }).catch((error) => {
      log.debug('Failed returning from Promise.resolve - v2 properties API');
    });
  } else {
    return res.status(500).json({status: 'ERROR', message: 'Internal Server error'});
  }
});

【讨论】:

    【解决方案2】:

    您可以使用路由的下一个参数/回调来等待承诺和/或其他异步操作。对于您的示例(未测试):

    router.get('/', (req, res, next) => {
      log.debug('api - v2 - Koko Version api call');
      if (req.query.prop) {
        serverStatus.transform(req).then((data) => {
          switch (req.query.prop) {
            case 'KokoVersion': {
              res.status(200).json({KokoVersion: data.version});
            }
            case 'KokoType': {
              res.status(200).json({KokoType: data.deviceType});
            }
            case 'LastChangeTime': {
              res.status(200).json({LastChangeTime: data.lastApply});
            }
          }
          next();
        }).catch((error) => {
          log.debug('Failed returning from Promise.resolve - v2 properties API');
          res.status(500).json({status: 'ERROR', message: 'Internal Server error'});
          next();
        });
      }
    });
    

    【讨论】:

    • 谢谢,这正是我需要的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2019-03-16
    相关资源
    最近更新 更多