The response from Firebase's Cloud Functions is triggering a 500 error code

I'm trying to run this function:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cors = require('cors')({origin: true});
const router = new express.Router();

 router.use(cors);  
 router.get('*', (req, res) => {
      res.setHeader('Access-Control-Allow-Origin', '*').status(200)
            .send('Hello, Functions');
    });

exports.date = functions.https.onRequest(router);

However, I encountered the following error:

XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. The response had HTTP status code 500.

Answer №1

If you're looking to accomplish a specific task, you'll need the following code:

const tools = require('firebase-tools');
const manager = require('firebase-manager');
manager.initializeApp(tools.settings().firebase);

// Begin setting up Firebase Functions
// Visit https://firebase.google.com/functions/write-firebase-functions

exports.today = functions.https.onRequest((req, res) => {
    res.send("Hi there, Functions");
})

Next step is to visit this link: .

Please note that Access-Control-Allow-Origin is restricted.

Answer №2

To achieve what you are attempting to do, the correct code would be:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.date = functions.https.onRequest((request, response) => {
    response.send("Hello, Functions");
})

An issue may arise with an error message similar to this:

XMLHttpRequest cannot load https://us-central1-idntt-baed5.cloudfunctions.net/date. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://idntt-baed5.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 500.

Please add the following header in your xhr request:

req.setRequestHeader('Access-Control-Allow-Origin', '*');  

Try testing in Mozilla Firefox as Google Chrome may not support it due to security reasons.

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

Interval set does not refresh an integer

I'm struggling with a function that is supposed to show the number of milliseconds elapsed since Midnight on January 1st, 1970. I've been trying to use setInterval to update the integer every second or millisecond, but it doesn't seem to be ...

Navigating through an array in jquery that has been populated by a php script

I am trying to access an array in jQuery that is returned by a PHP script through an Ajax call This is the Ajax call I am making to get the result from the PHP script: $.ajax({ url: "http://localhost/WCPM/index.php/demand/check_demand_ ...

SolidJS createEffect will only trigger on changes after the initial rendering

When I was searching for a solution, I came across the need to only trigger a reaction after my component had been rendered for the first time. For instance: function InputName() { const [name, setName] = createSignal(""); const [nameError, ...

Attributes of an object are altered upon its return from a Jquery function

After examining the following code snippet: index.html var jsonOut = $.getJSON("graph.json", function (jsonIn) { console.log(jsonIn); return jsonIn; }); console.log(jsonOut); The graph.json file contains a lengthy JSON fo ...

Retrieving Data with AngularJS: Making HTTP Calls and Handling Responses

I seem to be facing a bit of a hurdle in executing an HTTP GET request and retrieving the data properly in JavaScript. The goal is to attach this data to the global window variable, but I'm encountering some difficulty. Here is the code snippet for t ...

Discover the magic of triggering events that dynamically alter CSS styles

I am trying to implement an eventBus in the App.vue component that allows me to change a modal's CSS based on a payload object. For example, if I pass { type: 'success' }, the border of the modal should turn green, and if I pass { type: &apo ...

The jspdf function is not displaying any images in the PDF file

I am new to coding and encountered an issue when trying to download images in reactjs. After clicking the download button, I only receive a blank pdf file. https://i.stack.imgur.com/0XJcC.png The function in my react code that attempts to generate the p ...

Unable to update FB Login button to display "Logout" after user logs in to FB app

As a junior developer, I'm currently working on integrating Facebook login functionality into my React application following the quickstart guide provided by Facebook developers. However, I've encountered some issues with making the necessary mod ...

What causes errors in my AJAX request based on the particular file extension?

Utilizing JavaScript and jQuery, I have a function that loads data using $.get(url, function(response){ /* ... */});. The data is retrieved from a text file and handled within the response function of the JavaScript. Recently, I encountered an issue on my ...

Incorporate additional query parameters into a dynamic route with React Router to enhance functionality

I am currently working on incorporating an optional query parameter to the end of a path in order to create URLs like this: "/user/1/cars?makeYear=2020" or "/user/1/cars". The relevant Route is defined as shown below. I have been having ...

Confirm the object received from the API and assign default values

Seeking to extract data from an API and verify if all fields are strings, but if they are missing I aim to assign default values. My intention was to utilize the yup library to validate the object accordingly, ensuring that the returned function is prope ...

Encountered issue in Eclipse: Unable to perform dex operation due to method ID exceeding the limit of [0, 0xffff]. Error code

Struggling to implement firebase into my android app for reading data from the firebase database. Utilizing Eclipse for Android development, encountered a hurdle after adding the firebase-client-android-2.5.2.jar library – unable to run the application d ...

Ways to dynamically generate a card using NuxtJS

I'm just starting out with NuxtJS and I'm curious about how to generate a v-card within a v-dialog box. Imagine this scenario: I have an "add" button that triggers a v-dialog, where I can input information into a form. After submitting the form, ...

Modify button behavior on click after the initial press

Struggling to make this work the way I want. I have a button that should execute Javascript function A when clicked, and in function A, I need to change the onclick attribute to function B. This way, on the second tap of the button, it will trigger functio ...

Creating tags in HTML and displaying them on a webpage

I have a code that sends a message from a textarea after completion tags are entered as I wrote. The desired output should be: <h1> Thanks </h1> The expected output is: Transmitter Thanks instead of <h1> Thanks </h1> ...

Exploring the functionality of the JQuery Chained plugin for bidirectional chaining

Let's tackle a simple problem involving car brands and models. Take the example below: <select id="brand"> <option value="">Choose</option> <option value="audi" class="a1 a3 a4">Audi</option> <option value ...

Check if a Firestore document exists and display a button in Angular based on the boolean result

Seeking desired outcome: How to display a button in an Angular view based on the boolean value returned by an Angular Service that checks for the existence of a Firestore document. Current Scenario The Service successfully verifies the presence of t ...

retrieving and presenting information stored in a Firebase repository

I am currently working on retrieving data from a firebase database and storing it in an array called courses that I have declared within my class. Here's the code I have so far: import { AngularFireDatabase, AngularFireList } from 'angularfire2 ...

Issue with Angular 2: Unable to display 404 error page

I have created a custom "404 - page not found" page to handle situations where the user enters a URL that does not match any paths in my web app: export const routerConfig: Routes = [ { component: LandingPageComponent, path: "", }, { ...

JQuery restricts input to only allow numbers with a set number of digits, ensuring uniqueness

Can someone assist me in creating a jQuery script that only allows unique numbers with exactly 4 digits in a numeric field? Here is the HTML code: <input type="text" id="registration_number" class="input-sm" name="registration_number" maxlength="6" re ...