Firebase Integrations: How to Handle Errors in External API Calls within Firebase Functions

My current challenge involves retrieving data from an external API using Firebase functions and displaying it in an Angular application hosted on Firebase. Billing is enabled for the project. The API call works fine on localhost, but fails to fetch data when deployed on Firebase.

const functions = require('firebase-functions');

const express = require('express')
  , https = require('https')
  , bodyParser = require('body-parser')
  , app = express()
  , cors = require('cors');

app.use(cors());
app.use(bodyParser.json());

const options = {
  host: 'api.clashofstats.com',
  port: 443,
  path: '/clans/YUPJGL2R/',
  headers: {
    accept: "application/json",
  }
};

app.get("/clan", (req, res) =>{
  const request = https.request(options, (response) => {
    let httpResult = '';
    response.on('data',  (chunk) => {
      httpResult += chunk;
    });
    response.on('end', () => {
      res.send(httpResult);
    });
  });

  request.on('error', (e) => {
    console.log('problem with request: ' + e.message);
  });

  request.end();
});

exports.app = functions.https.onRequest(app);

console log

ERROR e {headers: t, status: 200, statusText: "OK", url: "https://united-clan.cz/clan", ok: false, …}

firebase log

Function execution took 10 ms, finished with status code: 404

network

Request URL: https://united-clan.cz/clan
Request Method: GET
Status Code: 200  (from disk cache)
Remote Address: 151.101.65.195:443
Referrer Policy: no-referrer-when-downgrade

https://us-central1-ucweb-224315.cloudfunctions.net/app 

Cannot GET null

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pages in Angular are being loaded multiple times when they are loaded with various parameter values

I have encountered an issue with my page setup that involves contracts and chats. Each chat corresponds to a specific contract, and depending on which chat or contract is open, the URL changes accordingly: david/contracts -> david/contracts/12 david/c ...

Error message stating NullInjectorError with NgxSpinnerService; encountered No provider for t while attempting to host on Firebase

As I attempt to deploy my app on Firebase, everything functions properly in localhost. However, upon successful hosting on Firebase at the Firebase domain, an issue arises: NullInjectorError: StaticInjectorError(wo)[class{constructor(t,e) at SpinnerServic ...

TypeError: The firestore function within the firebase_app__WEBPACK_IMPORTED_MODULE_0__ object is not recognized in Vue.js

I encountered a problem with my firebase.js file where I received the error message Uncaught TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0__.firestore is not a function in my console. Despite trying different methods to import firebase, none of them ...

Inadequate data being sent to the server from Angular2 post request

Currently, I have a form field whose value I am passing to a service as this.form.value. However, when I log this.form.value on the console, I see Object { email: "zxzx", password: "zxzxx" }. Despite this, when I send the same data to the service and make ...

Encountering an error with Angular2 when referencing node modules

I encountered an issue when trying to use angular2 from the node_modules directory. Can anyone guide me on how to resolve this error? Is there something specific I need to include in my HTML file? I am looking for a Git repository where I can access Angu ...

Mastering the art of utilizing Angular Material's custom-palette colors for maximum impact. Unle

I have implemented a custom material-color palette where I defined the primary and accent palettes with specific shades as shown below: $my-app-primary: mat-palette($md-lightprimary ,500,900,A700 ); $my-app-accent: mat-palette($md-lightaccent, 500,900 ...

Guide to testing my-sql database routes in an express application

Trying to test my NodeJS REST API has been a challenge due to most of my routes involving calls to a MySQL database. I've considered mocking or stubbing the database using Sinon, but have not had much success. Making real database calls in tests is no ...

Using Jquery to iterate through a PHP array, calling an API to fetch data, and then storing the results

I am working with a PHP array called startups[] that contains 36000 startup names. My goal is to create a URL API endpoint using these names. Despite my efforts, I have been unsuccessful in utilizing jQuery to iterate through this PHP array and for each o ...

I am attempting to develop a basic express application, but it doesn't appear to be functioning as expected

I am currently working on developing a straightforward express application. However, I am facing network errors when trying to access it through my browser at localhost:3000 while the application is running in the console. The root cause of this issue elud ...

Creating a personalized validation function in Angular to validate a form field against another form field

Below is the TypeScript code for the EtcAddAuthorityComponent: export class EtcAddAuthorityComponent implements OnInit { // Code here... } The HTML code for the component is as follows: <h1 mat-dialog-title>Add Authority</h1> <div mat-di ...

How can I manage asynchronous calls when querying Mongoose for each element in an array using Express?

Having trouble with the post method below because of a mongoose async issue. The code res.send(suggestions) is being executed before Expense.findOne.exec. app.post('/suggestions', async function(req, res) { const suggestions = await req.body ...

Add integer to an array of strings

Currently, I am utilizing an autocomplete feature and aiming to save the IDs of the selected users. My goal is to store these IDs in a string array, ensuring that all values are unique with no duplicates. I have attempted to push and convert the values u ...

Encountering a problem while implementing ngFor in Angular 4

Issue with ngFor Loop in Angular 4 Does anyone have insight into why this error is appearing in the console despite data being displayed? The data is showing, but an error is still being thrown empresas = <Empresa> {}; constructor(private servi ...

Implementing logout in a Node.js and Express application with Auth0: A step-by-step guide

I am currently working with a Node.js and Express server, leveraging Auth0 for authentication. I have been trying to figure out how to enable logout functionality when the client accesses the "/exit" route in my application. Here is an overview of the rele ...

Getting started with ExpressJS and Firebase Auth: beginner's guide to querying data

Currently, I am in the process of learning ExpressJS by developing a simple website that includes login functionality. My plan is to integrate Firebase for both account handling and database management using Firestore. One challenge I am facing is that Fi ...

expressjs templates

I am looking to implement two different layouts for the main page and admin page. What steps should I take to configure my code to achieve this? Here is my current configuration: app.configure(function(){ app.set('views', __dirname + ...

I'm sorry, but it seems like my hands are tied - unable to set headers after being sent

Whenever I attempt to log in with my email and password on NodeJS, an error pops up as shown below: (Partial excerpt) Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_htt ...

Updating view with *ngIf won't reflect change in property caused by route change

My custom select bar has a feature where products-header__select expands the list when clicked. To achieve this, I created the property expanded to track its current state. Using *ngIf, I toggle its visibility. The functionality works as expected when cli ...

Issue arises when the backend is running causing errors in the ReactJS frontend

I am encountering an issue with my React Js frontend and node express backend setup. When I run the frontend using npm start, everything works fine. However, when I also start the backend, I get this error: TypeError: Cannot read property 'map' o ...

Unable to redirect Firebase Hosting root to a Cloud Function successfully

Currently I am utilizing Firebase Hosting along with a Firebase.json file that is configured to direct all traffic towards a cloud function (prerender) responsible for populating meta and og tags for SEO purposes. { "hosting": { "public": "dist/pr ...