从中间件响应中解构赋值对象 [英] Destructuring assignment object from a middleware response

查看:105
本文介绍了从中间件响应中解构赋值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个中间件,如下所示:

I created a middleware as below:

const oAuth = (req, res, next) => {

    axios.post(tokenEndpoint, "", { params: {

        grant_type: process.env.GRANT_TYPE,
        client_id: process.env.CLIENT_ID,
        client_secret: process.env.CLIENT_SECRET,
        code: process.env.AUTHORIZATION_CODE,
       
    
    }}).then(response => {
        req.oAuth = response.data;
        //console.log(response.data);
        console.log(req.oAuth);
    }).catch(err => {
        console.error(err);
    })
    next();
}

module.exports = oAuth;

oauth 函数的响应/结果类似于:

the response/result of the oauth function is something like:

{
  access_token: '1000.ddf6d96b4f3sadasdasdas55a2450ae13',
  refresh_token: '100dasdsadasdsadhgdhgfhdghdfghe427288',
  api_domain: 'https://www.oapis.com',
  token_type: 'Bearer',
  expires_in: 3600
}

现在在index.js"中我正在尝试解构 oAuth 函数响应对象以访问属性 access_token 并将其放入 URL 以发出发布请求,但我没有成功.我做错了什么?

now in the "index.js" file I'm trying to destructure the oAuth function response object to access the attribute access_token and put it in the URL to make a post request, but I'm not succeeding. what am I doing wrong?

const express = require("express");
const axios = require("axios");
const oAuth = require("./oAuth.js");

const app = express();

app.use(oAuth);

var port = process.env.PORT || 3001;

const someAPI = "https://www.oapis.com/crm/v2/Leads";

app.get("/", async (req, res) => {
   
    try {
        const {access_token} = req.oAuth
                
        
        const response = await axios({
            method: "GET",
            url: someAPI,
            //timeout: 1000,
            headers: { Authorization: `Zoho-oauthtoken ${access_token}` },

        });
        return res.json(response.data);
        
    } catch (error) {
        console.log(error);
        if (error.response.status === 401) {
          res.status(401).json("Unauthorized to access data");
        } else if (error.response.status === 403) {
          res.status(403).json("Permission denied");
        } else {
          res.status(500).json("Whoops. Something went wrong");
        }
    };
});

推荐答案

我建议在这里使用 async/await 来等待 oauth 响应,然后才修改请求对象以便将其进一步传递给 next() 回调.

I would suggest using async/await here to wait for oauth response and only then modify request object in order to pass it further to the next() callback.

const oAuth = async (req, res, next) => {
    try {
        const response = axios.post(tokenEndpoint, "", { params: {
            grant_type: process.env.GRANT_TYPE,
            client_id: process.env.CLIENT_ID,
            client_secret: process.env.CLIENT_SECRET,
            code: process.env.AUTHORIZATION_CODE,    
        }});
        console.log(response.data);
        req.oAuth = response.data;
    } catch (e) {
        console.log(e);
    }
    next();
}

module.exports = oAuth;

这篇关于从中间件响应中解构赋值对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆