简体   繁体   English

我怎样才能等待我的“请求”和响应回到前端?

[英]How can I wait for my "request" and response back to front end?

I am working on the Zoom API now.我现在正在研究 Zoom API。 I want to send my token from ZOOM API to front-end as a response.我想将我的令牌从 ZOOM API 发送到前端作为响应。 However, "token from request" is always printed first and undefined!但是,“来自请求的令牌”始终首先打印且未定义! Then the token from Zoon API" will be followed with the token. How can I make it happen? Thx!然后来自 Zoon API 的令牌”将跟随令牌。我怎样才能做到这一点?谢谢!

const Newrequest = require("request");

class ZoomController {
  async getInvLink({ request, response }) {
const instructor_id = params.id;
try {
  let tokenRes;
  const code = request.all().code;
  console.log("the code from frontend is ", code);
  const client_id = _something_
  const client_secret = _something_
  var options = {
    method: "POST",
    url: "https://api.zoom.us/oauth/token",
    qs: {
      grant_type: "authorization_code",
      code: code,
      redirect_uri: _something_
    },
    headers: {
      Authorization:
        "Basic " +
        Buffer.from(client_id + ":" + client_secret).toString("base64")
    }
  };

  await Newrequest(options, function(error, res, body) {
    if (error) throw new Error(error);
    tokenRes = JSON.parse(body);
    console.log("token from Zoon API",tokenRes);
  });

  console.log("token from request",tokenRes);
  return response.status(200).send(tokenRes);
} catch (error) {
  console.log(error);
  return response.status(401).send();
}

I have no idea what this api is, but I'm going to make an educated guess that Newrequest doesn't return a promise.我不知道这个 api 是什么,但我将做出有根据的猜测, Newrequest不会返回承诺。 So awaiting it isn't actually what you want to do.所以等待它实际上并不是你想要做的。

What you can do, however, is use some simple code to turn it into a promise:但是,您可以做的是使用一些简单的代码将其转换为承诺:

const tokenRes = await new Promise((resolve, reject) =>  {
    Newrequest(options, function(error, res, body) {
      if (error) reject(error);
      tokenRes = JSON.parse(body);
      console.log("token from Zoon API",tokenRes);
      resolve(tokenRes);
    });
})

You would have to listen to an endpoint at something and you will receive the code over there.你必须要听的东西端点,您将收到的代码在那边。 This is the code you can send to exchange for an access token.这是您可以发送以交换访问令牌的代码。

Please consult this link : https://www.npmjs.com/package/request#promises--asyncawait请参阅此链接: https : //www.npmjs.com/package/request#promises--asyncawait

You can convert a regular function that takes a callback to return a promise instead with util.promisify()您可以使用util.promisify()将需要回调的常规函数​​转换为返回承诺

Example :例子 :

Newrequest(options, function(error, res, body) {
    if (error) throw new Error(error);
    tokenRes = JSON.parse(body);
    console.log("token from Zoon API",tokenRes);
});

// to

const util = require('util');

const req = util.promisify(Newrequest)
const data = await req(options)
// ... 

It's a sample code.这是一个示例代码。 Please adapt with your needs请适应您的需求

Useful course : https://masteringjs.io/tutorials/node/promisify有用的课程: https : //masteringjs.io/tutorials/node/promisify

Request library is deprecated请求库已弃用

It would be interesting to use another library.使用另一个库会很有趣。

Alternatives :备择方案 :

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM