axios request Error: connect ECONNREFUSED

19,232

You can't make requests to localhost when running on Cloud Functions. That's never going to work. You're going to need a full proper URL for the host or service you're trying to contact, and it's certainly not going to be localhost. localhost always means IP address 127.0.0.1, which is the same machine where the request is originating. Once you've deployed this code, localhost becomes the Cloud Functions server instance where the code is running, not your desktop machine.

Share:
19,232
user8063037
Author by

user8063037

Updated on June 26, 2022

Comments

  • user8063037
    user8063037 almost 2 years

    axios request error in firebase cloud function. here is my code.

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

    the error is showing me Error: connect ECONNREFUSED 127.0.0.1:8000 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14) how can I solve it? help me please.