【问题标题】:Want to get the response of nested api call and then send back to my middle ware node js api想得到嵌套api调用的响应,然后发回我的中间件节点js api
【发布时间】:2019-04-24 17:42:06
【问题描述】:

我想在嵌套的 api 调用中使用异步等待,但不知道怎么做?所以第二个 api 的响应发送回我从前端得到的 \api\sign_in 响应,这里有什么解决方案是我的代码: ....

app.post("/api/sign_in", (req, res) => {

          console.log("i am in login call");
          const resp = scaryClown(req.body.email, req.body.password);
          console.log("here you should wait for above response and then pass 
             this  response  into /api/sign_in response");
               res.send(resp);
 });

function scaryClown(email, password) {
request.post(
  key.sign_in_url,
  {
    json: {
      email: email,
      password: password
    }
  },
  function(error, response, body) {
    if (!error && response.statusCode == 200) {
      email = response.headers.uid;

      key.email = response.headers.uid;
      key.client = response.headers.client;
      var result = [];
      for (var i in response.headers) result.push(i, response.headers[i]);
      access_token = result[17];
      key.access_token = access_token;
      key.expiry = response.headers.expiry;

      apiresponse = response.body;
      console.log("apiresponse");
      console.log(apiresponse);
      return apiresponse;
    }

    console.log("==============");
    console.log(response.body);
    return response.body;
  }
);
}

....

【问题讨论】:

  • 要使用异步/等待,您需要返回承诺的请求库版本。请求库文档中有建议。否则添加一个回调参数到scaryClown()

标签: javascript node.js api post async-await


【解决方案1】:

要等待您的可怕小丑函数的响应,您可以在之前使用 await 你的函数调用。

app.post("/api/sign_in", async (req, res) => {

    console.log("i am in login call");
    const resp = await scaryClown(req.body.email, req.body.password);
    console.log("here you should wait for above response and then pass 
          this  response  into / api / sign_in response");
            res.send(resp);
});

function scaryClown(email, password) {
  return new Promise((resolve, reject) => {
    request.post(
        key.sign_in_url,
        {
            json: {
                email: email,
                password: password
            }
        },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                email = response.headers.uid;

                key.email = response.headers.uid;
                key.client = response.headers.client;
                var result = [];
                for (var i in response.headers) result.push(i, response.headers[i]);
                access_token = result[17];
                key.access_token = access_token;
                key.expiry = response.headers.expiry;

                apiresponse = response.body;
                console.log("apiresponse");
                console.log(apiresponse);
                resolve(apiresponse);
            }

            console.log("==============");
            console.log(response.body);
            resolve(response.body);
        }
    );
  });
}

【讨论】:

  • 我已经尝试过这种方法,但这不起作用,因为在节点中我得到空响应我想得到我可怕小丑的响应然后这个响应传递给 sign_in api
  • 好的,在这种情况下,您可以从您的可怕小丑函数返回一个承诺。它应该工作。我在回复中编辑了代码。
猜你喜欢
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
相关资源
最近更新 更多