A guide on incorporating Firestore into Firebase functions using Express

Currently, I am engaged in a project which involves using Express in Firebase functions to execute server-side rendering pages with the help of Handlebars. Everything is running smoothly, but the problem arises when I incorporate the Firestore admin SDK and encounter the following error:

SyntaxError: Invalid or unexpected token
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)

The code snippet causing this issue can be found in my functions/index.js file

const functions = require('firebase-functions');
const express = require('express')
const engines = require('consolidate')
const cookieParser = require('cookie-parser')
const serviceAccount = require('./lendme360-firebase-adminsdk-easkh-d9130c0494.json');
var admin = require('firebase-admin')
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
})

const stripe = require('stripe')('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
const db = admin.firestore()
const app = express()
app.engine('hbs', engines.handlebars)
app.set('views', './views')
app.use(cookieParser())
app.set('view engine', 'hbs')
...
...
...
...
...
exports.app = functions.https.onRequest(app)

The functionality seems to be working fine overall. However, the error occurs specifically when attempting to use const db = admin.firestore(). I have also experimented with emulators for both functions and firestore without resolution.

I would greatly appreciate any assistance in identifying where I may be going wrong here.

Thank you in advance.

Answer №1

It appears that the issue you're experiencing is due to the module not being loaded in your current environment. I see that you mentioned using different environments to test this problem.

In a local environment, try running a test where you initialize the module with "const db = admin.firestore()". You may encounter the same error as before, but then you can try running "npm install firebase-admin --save" as outlined in this documentation. This will enable the module to be used with node js.

If you are working with Firebase functions, ensure that you have defined your modules in your package.json according to this other documentation. This should resolve the error message indicating that Node is having trouble recognizing the syntax or command from the module (due to it not being loaded).

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

The variable 'angular' was not defined in the JavaScript file within the Node Express project in Eclipse

I am looking to implement Angular.js as the front end in my Node Express Project developed in Eclipse IDE. Whenever I attempt to move my angular controller into a separate Javascript file, I encounter errors such as 'angular' is not defined and C ...

Modifying background image in React based on user input value - Here's how!

Is there a way to dynamically change the background image of a website based on user input? I want the background image to update to reflect specific keywords entered by the user. For example, if the user types in "New York", "New York City", or "NYC", the ...

Please upload JSON data into a database using Node.js

As a beginner in learning node.js, I have embarked on my "Hello World!" project. The premise is simple - I am requesting a JSON file from the server via an API and receiving a response that looks like this: { "files": [{ "url": "http://auction ...

Switch from a class component to a functional component in React JS by clicking a button

I am currently facing an issue with navigating from a React.js class component to a functional component and passing data. Here is the structure I am working with: Class component: export default class Dept extends Component { constructor(props) {}; ...

What is the best way to display these images correctly within a slider?

My goal is to display 4 images/company logos per slide, but they all end up clustered on one slide. Despite my efforts to adjust the CSS, nothing seems to work. This is what my code currently renders: https://i.sstatic.net/ThaOK.png Here are the react co ...

Issue with displaying a personalized error message using the error handling middleware on Express.js

The code I have seems to have an error handling middleware that should be called when localhost:3000 is opened on the browser. However, instead of getting the response 'system is unavailable for the moment', I receive the error message "my custom ...

How to disable sorting on the top LI element using jQuery?

I currently have two separate UL elements that are being sorted using the jQuery Sortable plugin. Each UL contains between one and ten LI elements arranged in a list format. $(".sortable").sortable({ connectWith: ".sortable", cancel: '.no-dra ...

Utilizing NodeJS to efficiently retrieve MySQL data via query URLs

Hey everyone, I have a question about using single and double quotes in JavaScript with NodeJS and MySQL. Here is the code snippet: app.get('/AddCollection', function (req, res) { var queryData = url.parse(req.url , true).query; connecti ...

What causes the "Cannot read properties of undefined" error in Vue when an array is initialized and output is still being displayed?

I am working with two vue files, namely App.vue and a component called UsersPage.vue. UsersPage.vue: <script> export default { data:() =>({ usersList : [] }), methods:{ async createUsersList(){ this.usersLi ...

Elements in list appear to break apart when resizing

I had previously posted about this issue, but I deleted it after making some progress. I am currently facing a problem with my todo list project when resizing the screen. My project involves using JavaScript to create a new list item for each entry. Users ...

What is the best way to extract an object by its specific id using json-server?

I am attempting to retrieve an object by its id using json-server. If I only return a single variable (one JSON) in the module.exports of the database file, everything works fine. However, if I try to return an object with multiple variables, I encounter a ...

How can I send an array (or TList) from C# code behind to an external javascript file?

Is there a recommended method for passing a long list of IP addresses and accompanying data from C# code behind to an external JavaScript function? The list could potentially contain over 1000 items, so efficiency is key. Can you offer a concise example ...

Steps to retrieve a file following an AJAX request

Is it possible to automatically download a file following an AJAX call? Currently, when I make the AJAX request, it redirects me to the resource instead of initiating the file download. This is the AJAX function I am using: $('.download').click ...

Exploring the basics of Three.js: a step-by-step guide using a shadertoy example on transforming patterns with objects

Check out this snippet, inspired by the second live example found at : html, body { height: 100%; margin: 0; } #c { width: 100%; height: 100%; display: block; } <canvas id="c"></canvas> <script type="module"> // Three.j ...

Convert JavaScript object into distinct identifier

I have a data object where I'm storing various page settings, structured like this: var filters={ "brands":["brand1","brand2","brand3"], "family":"reds", "palettes":["palette1","palette2","palette3"], "color":"a1b2" }; This object is ...

Retrieve all dynamically created select elements using jQuery

I am using jQuery to retrieve and send data via ajax. I have select elements that can be dynamically generated by the user based on their needs. When any of these selects are changed, I want to collect all their values for later use. Unfortunately, I enc ...

Setting the input value using downshift in ReactJS can be accomplished by following these steps

Currently, I am in the process of creating an autocomplete input text box using downshiftjs. However, I am facing an issue where I cannot set the input text when one of the autocomplete results is clicked. If you would like to view the sample code that I& ...

Enable contenteditable on table by pressing the tab key

I developed a table entry tool and I would like to be able to input items by pressing the tab key instead of having to manually click on each element. I haven't been able to figure out how to make this function work yet. $('table').on(&apos ...

Using global variables for mocha testing (and babel setup)

Currently, I am developing a library using es6 and transpiling it with babel via webpack and npm. However, I have encountered an issue where my library has a dependency on some code that cannot be modified but is required for my library to function properl ...

A guide on populating a dropdown menu with spring and hibernate in JSP

I'm a newcomer here and seeking ideas from you all. I have 4 dropdown lists and want to populate one select box based on the selection made in another select box using database values. I have already set up the database, but unsure how to proceed. Any ...