【问题标题】:Using axios in a web action in IBM Cloud using node.js使用 node.js 在 IBM Cloud 中的 Web 操作中使用 axios
【发布时间】:2021-04-08 14:55:01
【问题描述】:

我一直在尝试获得一个简单的网络操作来向 API 发出经过身份验证的 GET 请求(我已经从示例代码中删除了实际的 url 和秘密)。

我已经在本地成功运行了这个,但是当我测试 web 操作时,它在记录“调用 axios”后就死了。

它没有报错,我试图实现一个promise,认为线程在api响应之前就结束了,但没有效果。有什么指点吗?

    /**
      *
      * main() will be run when you invoke this action
      *
      * @param Cloud Functions actions accept a single parameter, which must be a JSON object.
      *
      * @return The output of this action, which must be a JSON object.
      *
      */
    function main(params) {
    getData().then(function(result) {
        console.log("In the THEN of original method call");
    return "hello";
        
    })
    .catch(function(err) {
        console.log("In catch of original method call");
    });
    
        
    }
    
     function getData(){
                    const crypto = require('crypto');
                    const axios = require('axios');
                    const secretKey = "ENTER KEY HERE";  
                    const apiId = 99999;  
                    const apiBaseUrl = "https://acmewebservice.com";
                    const apiPath = "/customer/9";
                    const apiFullPath = apiBaseUrl + apiPath;       
                    const sharedSecretHash = crypto.createHash('sha256').update(secretKey).digest('hex');
             
                    console.log("In getData");
                    var authToken = apiId + ":" + sharedSecretHash;
            
            return new Promise((resolve, reject) => {
                    console.log("Calling axios");
                    axios.get(apiFullPath, {
                        headers: {
                            'Authentication': authToken
                            }
                        }).then(response => {
                            console.log("Did this work?")
                            var x = JSON.stringify(response.data);
                            console.log(x);
                            resolve(response);
                      })
                        .catch(error => {
                            console.log("In Catch")
                        console.log(error);
                        reject(error);
                    });
            });

【问题讨论】:

    标签: node.js axios ibm-cloud


    【解决方案1】:

    你不需要重新包装 axios 调用,它已经是一个承诺。

    需要return 才能使引擎有效地等待异步调用的结果。

    
         function getData(){
                        const crypto = require('crypto');
                        const axios = require('axios');
                        const secretKey = "ENTER KEY HERE";  
                        const apiId = 99999;  
                        const apiBaseUrl = "https://acmewebservice.com";
                        const apiPath = "/customer/9";
                        const apiFullPath = apiBaseUrl + apiPath;       
                        const sharedSecretHash = crypto.createHash('sha256').update(secretKey).digest('hex');
                 
                        console.log("In getData");
                        var authToken = apiId + ":" + sharedSecretHash;
                
                        console.log("Calling axios");
                        return axios.get(apiFullPath, {
                            headers: {
                                'Authentication': authToken
                                }
                            }).then(response => {
                                console.log("Did this work?")
                                var x = JSON.stringify(response.data);
                                console.log(x);
                                return response;
                          })
                            .catch(error => {
                            console.log("In Catch")
                            console.log(error);
    
                        });
    
    

    【讨论】:

    • 是的。就是这样。非常感谢。
    • 太棒了@PaulGarden!你能“绿色检查”我的答案吗?还是那是管理员的事情?我刚回到这个网站。谢谢!
    猜你喜欢
    • 2023-03-08
    • 2019-09-06
    • 2021-03-18
    • 2020-08-29
    • 2019-01-26
    • 1970-01-01
    • 2023-03-10
    • 2021-10-18
    • 2017-01-15
    相关资源
    最近更新 更多