axios 请求错误:连接 ECONNREFUSED

use*_*037 4 node.js typescript google-cloud-functions

firebase 云函数中的 axios 请求错误。这是我的代码。

import * as functions from 'firebase-functions';
import axios from 'axios';

export const mobileDoUpdate = 
functions.firestore.document('/users/{ID}')
.onUpdate((snapshot, context) => {


axios.get('http://localhost:8000/user?id=29&status=active')
.then(response => {
    console.log(response.data.url);
    console.log(response.data.explanation);
})
.catch(error => {
    console.log(error);
});

});
Run Code Online (Sandbox Code Playgroud)

错误显示我 Error: connect ECONNREFUSED 127.0.0.1:8000 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14) 我该如何解决?请帮帮我。

Dou*_*son 10

在 Cloud Functions 上运行时,您无法向 localhost 发出请求。那永远行不通。您将需要一个完整的正确 URL,用于您尝试联系的主机或服务,而且它肯定不会是 localhost。localhost 始终表示 IP 地址 127.0.0.1,它与发出请求的机器相同。部署此代码后,localhost 将成为运行代码的 Cloud Functions 服务器实例,而不是您的台式机。