Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP request to a google service returns Error: read ECONNRESET firebase cloud functions

I am trying to translate the name of a user from english to an indian language using google translate api and storing the data back in realtime database with a cloud function.

This function is invoked by a write to the database, and I am using a HTTP POST request to send a request to the cloud translate api and the response is stored back to the database. My code for the translate request is this.

var translate_options = { method: 'POST',
                                  url: 'https://translation.googleapis.com/language/translate/v2',
                                  qs:
                                   { key: 'key goes here',
                                    },
                                    form: {
                                      q: fullData.name,
                                      target: "te"
                                    },
                                  };

                    request(translate_options, function (error, translate_response, translate_body) {
             if (error){
                          console.log("In translating, got an error");
                          console.log(error);
             }
             // Query to the database goes here.

  });

This code, if tried in my laptop, gives me the correct translation, but if I deploy it as a cloud function, it gives me an error. Very specifically

{ Error: read ECONNRESET
    at exports._errnoException (util.js:1020:11)
    at TLSWrap.onread (net.js:568:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

I am on firebase blaze plan, and I am able to sent POST request to my other services, but not a google service.

Can anybody help me with this issue. Thanks in advance.

Edit :

The full code is

var functions = require('firebase-functions');
var admin = require('firebase-admin');
var request = require("request");

admin.initializeApp(functions.config().firebase);


exports.whenUserIsAdded = functions.database.ref('users/{companyId}/{uid}').onCreate(event => {

var fullData = event.data.val();


var lang_code = {
  "bengali": "bn",
  "telugu": "te",
  "english": "en"
}

var lang_var = lang_code[fullData['edition']];


var translate_options = { method: 'POST',
              url: 'https://translation.googleapis.com/language/translate/v2',
              qs:
               { key: 'Key goes here',
                },
                form: {
                  q: fullData.name,
                  target: lang_var
                },
              };

request(translate_options, function (error, translate_response, translate_body) {
   var farmer_name = "";
   if(error){
        console.log("There is an error in translation");
        console.log(error);
   }
  translate_body = JSON.parse(translate_body);

  if(translate_body.data.translations){
    farmer_name = translate_body.data.translations[0].translatedText;
    console.log("The farmer name is " + fullData.name +"  :  " + farmer_name);
    // Code to write to the database;
  } else{
    console.log("The translation failed");
    farmer_name = fullData.name;
    console.log("The famrer name is  " + farmer_name);
  }

})

});
like image 880
Raghavendra Gautam Avatar asked Sep 14 '25 12:09

Raghavendra Gautam


1 Answers

You're not returning a promise that's resolved when all the work of your function is complete. If the work was completing in the past, that possibly just means you were lucky. Without returning a promise, Cloud Functions may terminate and clean up any work that wasn't complete when the function returns. Properly returning a promise will prevent Cloud Functions from cleaning up before the work is done.

Please consider reading my blog post about this. There is a section special just for ECONNRESET.

like image 94
Doug Stevenson Avatar answered Sep 16 '25 16:09

Doug Stevenson