Как передать ответ одного API в качестве параметра запроса в другой API с помощью Request-Promise - PullRequest
0 голосов
/ 05 октября 2018

Я хочу передать ответ, полученный от одного API, в качестве параметра запроса в другом API с помощью модуля Request-Promise NodeJs.Может кто-нибудь, пожалуйста, помогите мне в этом?Я даю краткое описание кода ниже:

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?

Ответы [ 3 ]

0 голосов
/ 05 октября 2018

Вы можете использовать this, потому что у вас есть оператор return в методе test1().Итак, просто активируйте его, чтобы получить:

"qs": {
     "pairingInfo": this.test1(),
}
0 голосов
/ 05 октября 2018

Используйте эту функцию:

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;
}

};

0 голосов
/ 05 октября 2018
Sequence.test1(param)
.then(function(pairingInfo) {
   Sequence.test2(pairingInfo)  ;
});

// Вы возвращаете paringInfo с первым обещанием, поэтому вы можете использовать его в методе .then ().

...