将已获得(请求)的响应返回给对象以进行回调

问题描述 投票:0回答:1

我正在使用got模块发出POST请求以获取数据。我想同步调用3个API,获取响应对象,然后回调。我无法将响应返回给回调对象。它返回{}而不是实际响应。我还想同步进行所有这些调用,以便同时进行请求以节省时间。

const got = require('got');

exports.handler = (event, context, callback) => {
    
    async function getDATA1() {
    	return await got.post('https://url.com/serviceapi/1/', {
    	    responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
    		json: {
                "Input": {
                    "item":1,
                }
            }
    	}).json();
    }

    async function getDATA2() {
    	return await got.post('https://url.com/serviceapi/2/', {
    	    responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
    		json: {
                "Input": {
                    "item":2,
                    "color":"blue",
                }
            }
    	}).json();
    }

    async function getDATA3() {
    	return await got.post('https://url.com/serviceapi/3/', {
    	    responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
    		json: {
                "Input": {
                    "item":3,
                    "size":"large",
                }
            }
    	}).json();
    }
    
    callback(null,{
        "DATA1": getDATA1(),
        "DATA2": getDATA2(),
        "DATA3": getDATA3(),
    });
};

我得到的答复是

{
  "DATA1": {},
  "DATA2": {},
  "DATA3": {}
}

我期望/想要的回复是这样的

{
  "DATA1": {
               "available_f":"Y"
           },
  "DATA2": {
               "available_f":"N"
           },
  "DATA3": {
               "available_f":"Y"
           },
}
node.js asynchronous post lambda request
1个回答
1
投票

这些异步函数将需要等待,因此您需要执行类似的操作,直到所有请求都解决之后,您才能收集结果并通过回调返回它们。

const got = require('got');

exports.handler = (event, context, callback) => {

    async function getDATA1() {
        return await got.post('https://url.com/serviceapi/1/', {
            responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
            json: {
                "Input": {
                    "item": 1,
                }
            }
        }).json();
    }

    async function getDATA2() {
        return await got.post('https://url.com/serviceapi/2/', {
            responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
            json: {
                "Input": {
                    "item": 2,
                    "color": "blue",
                }
            }
        }).json();
    }

    async function getDATA3() {
        return await got.post('https://url.com/serviceapi/3/', {
            responseType: 'json',
            resolveBodyOnly: true,
            username: 'user',
            password: 'pass',
            json: {
                "Input": {
                    "item": 3,
                    "size": "large",
                }
            }
        }).json();
    }

    Promise.all([getDATA1, getDATA2, getDATA3]).then(values => {
        return callback(null, {
            DATA1: values[0],
            DATA2: values[1],
            DATA3: values[2]
        })
    }).catch(err => {
        return callback(err);
    });
};
© www.soinside.com 2019 - 2024. All rights reserved.