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

222
Views
Can't get data from firebase in react chart

I'm using ApexChartJs for my react project, but when I tried to fetch dynamic data from my firebase database, then show me undefined.

enter image description here

here is my part of code from my project:

import React, { useEffect, useState } from "react";
import Chart from "react-apexcharts";
import { auth, db } from "./firebase";
import { useCollection } from "react-firebase-hooks/firestore";


  const [dataPoint, setDataPoint] = useState([]);
  const query = db.collection("data");
  const [snapshot, loading, error] = useCollection(query);
  const [series, setSeries] = useState([]);

  useEffect(() => {
    setSeries([
      {
        name: "Desktops",
        data: snapshot?.docs.map((doc) => doc.data().value),
      },
    ]);
  }, [snapshot]);

  console.log(series);

data in the firebase look like:

enter image description here

firebase configure file:

import firebase from "firebase";

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "AIzaSyC2mfjPdIdXxGgJFqqVviw32xoaEMxxxxxx",
  authDomain: "chart-app-4591b.firebaseapp.com",
  projectId: "chart-app-4591b",
  storageBucket: "chart-app-4591b.appspot.com",
  messagingSenderId: "995691438244",
  appId: "1:995691438244:web:6d945f72ff51d444664631",
  measurementId: "G-2B9ZFG6E33",
};

const app = firebase.initializeApp(firebaseConfig);

const db = app.firestore();
const provider = new firebase.auth.GoogleAuthProvider();
const auth = firebase.auth;

export { db, provider, auth };

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If your firebase app is well set-up and configured appropriately, the main issue that may lead an undefined query result is that the result are still being loaded.

To overcome this, you should update your series state whenever the loading property changes (along with query result snapshot changes):

  const query = db.collection("data");
  const [snapshot, loading, error] = useCollection(query);

  useEffect(() => {
    setSeries([
      {
        name: "Desktops",
        data: snapshot?.docs.map((doc) => doc.data().value),
      },
    ]);
  }, [loading, snapshot]);

You should provide alternative error handling as well for state update.

Update (per the permission error)

A common error source would be the lack of privileges to read and write from the configured Firestore DB highlighted by below error message:

FirebaseError: Missing or insufficient permissions

To allow all reads and writes for test only mode (should never be the case for a production system), you can set below Security Rules for the database:

// Allow read/write access to all users under any conditions
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

You can read more on Cloud Firestore Security Rules in the official documentation.

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!