Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用请求承诺将一个API的响应作为请求参数传递给另一个API_Javascript_Node.js_Node Request_Request Promise - Fatal编程技术网

Javascript 如何使用请求承诺将一个API的响应作为请求参数传递给另一个API

Javascript 如何使用请求承诺将一个API的响应作为请求参数传递给另一个API,javascript,node.js,node-request,request-promise,Javascript,Node.js,Node Request,Request Promise,我想使用request-Promise-NodeJs模块将从一个API收到的响应作为请求参数传递给另一个API。有人能帮我吗?下面我简要介绍一下示例代码: var Sequence = { test1: function (param) { return request({ "method": "POST", "uri": baseURL+"/v1/" + userID + "/test/

我想使用request-Promise-NodeJs模块将从一个API收到的响应作为请求参数传递给另一个API。有人能帮我吗?下面我简要介绍一下示例代码:

var Sequence = {

        test1: function (param) {
            return request({
                "method": "POST",
                 "uri": baseURL+"/v1/" + userID + "/test/info/",
                "json": true,
                "headers": {
                    "Accept": "application/json",
                },
            }).then(function (result) {
                return result.pairingInfo // I want to use this pairinfInfo param in another request
            })

test2 : function (param) {

           return request({
                "method": "POST",
                "uri": baseURL+"/v1/passenger/" + userID + "/test/test/",
                "json": true,
                "headers": {
                    "Accept": "application/json",
                },
                "qs": {
                    **"pairingInfo": pairingInfo**,//This pairingInfo would come from the returned result.pairingInfo of test 1 API call
                }
            })

        }
        },

How can I achieve this?

//您将返回带有第一个承诺的paringInfo,因此您可以在.then()方法中使用它。

您可以使用
this
,因为
test1()方法中有
return
语句。所以,只要触发它就可以得到它:

"qs": {
     "pairingInfo": this.test1(),
}
使用此功能:

const sequence = async (baseURL, userID) => {
try {
    let options1 = {
        method: 'POST',
        uri: baseURL + '/v1/' + userID + '/test/info/',
        json: true,
        headers: {
            Accept: 'application/json'
        }
    };

    let pairingInfo = await request(options1);
    if (pairingInfo) {
        let options2 = {
            method: 'POST',
            uri: baseURL + '/v1/passenger/' + userID + '/test/test/',
            json: true,
            headers: {
                Accept: 'application/json'
            },
            qs: {
                pairingInfo: pairingInfo //This pairingInfo would come from the returned result.pairingInfo of test 1 API call
            }
        };

        await request(options2);
        return true;
    } else {
        console.log('Request 1 failed');
        return false;
    }
} catch (err) {
    console.error(err);
    return false;
}

})

从sequence.test1本身返回pairinginfo,而不是从then函数返回
const sequence = async (baseURL, userID) => {
try {
    let options1 = {
        method: 'POST',
        uri: baseURL + '/v1/' + userID + '/test/info/',
        json: true,
        headers: {
            Accept: 'application/json'
        }
    };

    let pairingInfo = await request(options1);
    if (pairingInfo) {
        let options2 = {
            method: 'POST',
            uri: baseURL + '/v1/passenger/' + userID + '/test/test/',
            json: true,
            headers: {
                Accept: 'application/json'
            },
            qs: {
                pairingInfo: pairingInfo //This pairingInfo would come from the returned result.pairingInfo of test 1 API call
            }
        };

        await request(options2);
        return true;
    } else {
        console.log('Request 1 failed');
        return false;
    }
} catch (err) {
    console.error(err);
    return false;
}