I am trying to receive the data from an uploaded file from firebase firestore and save it in an array
but its throws me an "Uncaught (in promise) FirebaseError: Expected type 'Pa', but it was: a custom Va object"
Thanks for help
import { useState, useEffect } from "react";
import { projectFirestore } from "../firebase/config";
import { collection as col, onSnapshot, query, orderBy } from "firebase/firestore";
const useFirestore = (collection) => {
const [docs, setDocs] = useState([]);
useEffect (() => {
//new collection reference
const newcoll = (col(projectFirestore, collection))
const q = query(newcoll, orderBy('createdAt', 'desc'));
const unsub = onSnapshot(q, (snapshot) => {
let documents = [];
snapshot.forEach(doc => {
documents.push({...doc.data(), id: doc.id})
})
setDocs(documents);
});
return () => newcoll();
},[collection]);
CodePudding user response:
I would make a few changes in the code to be:
useEffect (() => {
if(collection) {
const newcoll = col(projectFirestore, collection);
const q = query(newcoll, orderBy('createdAt', 'desc'));
const unsub = onSnapshot(q, (snapshot) => {
let documents = [];
snapshot.forEach(doc => {
documents.push({...doc.data(), id: doc.id})
})
setDocs(documents);
});
return unsub;
}
},[collection]);
Changes include:
- returning
unsubinstead of() => newcoll() - checking that
collectionhas a truthy value
