javascript - 如何使用 Request-Promise 将一个 API 的响应作为请求参数传递到另一个 API 中

标签 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/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?

最佳答案

您可以使用 this,因为 test1() 方法中有一个 return 语句。因此,只需触发它即可获取它:

"qs": {
     "pairingInfo": this.test1(),
}

关于javascript - 如何使用 Request-Promise 将一个 API 的响应作为请求参数传递到另一个 API 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661464/

相关文章:

javascript - 正好有 3 个字符和 10 个数字的 13 个字符字母数字字符串的正则表达式

javascript - 检查鼠标点击情况

node.js - Socket.IO IPv6(2013 年 1 月版)

linux - 千个并发请求 - EAI_AGAIN/ECONNRESET 错误

Node.js 服务器错误 : Parse Error at socketOnEnd (_http_server. js:455:20)

javascript - Div 悬停无法正常工作

javascript - 如何计算现在日期和恒定日期之间的差异。并像 1d,1m,1w 一样编辑它

javascript - 如何在 Kotlin 中使用名为 "val()"的函数?/扩展外部 JS 类

node.js - 在 Express Node.js 中禁用 etag header

node.js - 类型错误 : Request path contains unescaped characters NodeJS + Dynamics