Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
unhandledRejection: ReferenceError: Cannot access 'firestore' before initialization

I'm very new to next.js, and I'm developing an API with it, side that API I want to use firebase, so I set up firebase but when I try to call it, this error appears: "unhandledRejection: ReferenceError: Cannot access 'firestore' before initialization"

Code below.

Firebase config file

import firebase from 'firebase/app';
import "firebase/firestore";

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "*******",
    authDomain: "********",
    projectId: "*****",
    storageBucket: "*****",
    messagingSenderId: "****",
    appId: "****",
    measurementId: "****"
  };

//if(!firebase.apps.length){
   
//}
firebase.initializeApp(firebaseConfig);
const firestore = firebase.firestore();
export { firestore };

Firestore service

import { firestore } from './firebase';

export class Firestore {
    async getOne(collection, uid){
        const data = await firestore.collection(`${collection}/${uid}`).get();
        return await data.docs();
    }
}

API page

import {Firestore} from '../../utils/firestore';
const firestore = new Firestore();

const getBanners = () => {
    return firestore.getOne('settings','client');
    //return data;
}

export default function Handler(req,res){
    res.status(200).json({
        'name':"getBanners",
        'data': getBanners()
    });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved this issue by adding import firebase from 'firebase/compat/app'; and also import 'firebase/compat/firestore';. And works!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!