Firestore: get an array of objects - javascript

Is it possible to get an array of objects from Firestore. I tried something like below but I am getting undefined when I tried to log comments[0].comment
let comments = [{}]
try {
const ref = firebase
.firestore()
.collection('comments')
.where('ytid', '==', id)
const commentSnapshot = await ref.get()
let comments = commentSnapshot
console.log('comment snapshot')
console.log(comments[0].comment) //undefined
} catch (e) {
console.log(e)
}

I have figured it out. I did it like below and it works.
let comments = []
try {
const ref = firebase
.firestore()
.collection('comments')
.where('ytid', '==', id)
const commentSnapshot = await ref.get()
commentSnapshot.forEach((doc) => {
var obj = {}
obj['comment'] = doc.data().comment
obj['createdat'] = doc.data().createdat
obj['username'] = doc.data().username
obj['name'] = doc.data().name
obj['photourl'] = doc.data().photourl
comments.push(obj)
})

This returns a QuerySnapshot which contains the DocumentSnapshot of each document that has matched your query.
const commentsSnapshot = await firebase.firestore().collection('comments').where('ytid', '==', id).get()
The array of object is a field in your document. You cannot get a single field from a document. You need to fetch the document and then access that field hence you make that query above first.
Now commentsSnapshot.docs is an array of DocumentSnapshots. Now if you know there is only one matching document you can access it's data like this:
const firstCommentData = commentsSnapshot.docs[0].data()
//Access a specific field
const anyField = firstCommentData.anyField
In case your QuerySnapshot has multiple documents, you can loop thought the docs as it is an array.
//commentsSnapshot.forEach(...) works as well
commentsSnapshot.docs.forEach((doc) => {
console.log(doc.data())
})

Related

Can't update firebase collection field - Expected type 'ya', but it was: a custom Ia object

I am trying to make barbershop web app where costumer can see list of free appointments and when they reserve free appointment I want to delete that field from firebase.
I have a collection which represents one barber.
This is how it looks in firebase.
As you see radno_vrijeme is object or map in firebase which contains 6 arrays, and in each array there is list of free working hours.
In my function I am able to do everthing except last line where I need to update firebase collection.
const finishReservation = async () => {
try {
const freeTimeRef = collection(db, `${barber}`);
const q = query(freeTimeRef);
const querySnap = await getDoc(q);
querySnap.forEach(async (doc) => {
const radnoVrijeme = doc.data().radno_vrijeme;
// Find the index of the hour you want to delete
const index = radnoVrijeme["Mon"].indexOf(hour);
// Remove the hour from the array
radnoVrijeme["Mon"].splice(index, 1);
// Update the document in the collection
console.log(radnoVrijeme);
const radnoVrijemeMap = new Map(Object.entries(radnoVrijeme));
await freeTimeRef.update({ radno_vrijeme: radnoVrijemeMap });
});
} catch (error) {
console.log(error);
}
};
I tried to pass it as JSON stringified object, but it didn't work. I always get this error :
"FirebaseError: Expected type 'ya', but it was: a custom Ia object"
When you are trying to fetch multiple documents using a collection reference or query, then you must use getDocs():
const finishReservation = async () => {
try {
const freeTimeRef = collection(db, `${barber}`);
const q = query(freeTimeRef);
const querySnap = await getDocs(q);
const updates = [];
querySnap.forEach((d) => {
const radnoVrijeme = d.data().radno_vrijeme;
const index = radnoVrijeme["Mon"].indexOf(hour);
radnoVrijeme["Mon"].splice(index, 1);
const radnoVrijemeMap = new Map(Object.entries(radnoVrijeme));
updates.push(updateDoc(d.ref, { radno_vrijeme: radnoVrijemeMap }))
});
await Promise.all(updates);
console.log("Documents updated")
} catch (error) {
console.log(error);
}
};
getDoc() is used to fetch a single document using a document reference.

How to access individual object in array using Javascript

Hi I have exported using data (hawkers collection) using getDocs() from Firebase.
After that I put each hawker data as an object in an array called allStall as shown in the screenshot of the console log below.
Question 1 - How do I access each individual object in my allStall array. I try to use .map() to access each of it, but i am getting nothing.
Do note that I already have data inside my allStall array, see screenshot above.
[Update] map doesn't work in code below because field is stallname not stallName. However, it needs to be async + await if using/call in/from other function.
Question 2 - Why is there [[Prototype]]: Array(0) in my allStall array
export /*Soln add async*/function getAllStall(){
var allStall = [];
try
{
/*Soln add await */getDocs(collection(db, "hawkers")).then((querySnapshot) =>
{
querySnapshot.forEach((doc) =>
{
var stall = doc.data();
var name = stall.stallname;
var category = stall.category;
var description = stall.description;
var stallData = {
stallName:name,
stallCategory:category,
stallDescription:description
};
allStall.push(stallData);
});});
console.log(allStall);
//Unable to access individual object in Array of objects
allStall.map(stall =>{console.log(stall.stallName);});}
catch (e) {console.error("Error get all document: ", e);}
return allStall;
}
In my main js file, i did the following:
useEffect(/*Soln add await*/() =>
{
getAllStall();
/*Soln:replace the statement above with the code below
const allStall = await getAllStall();
allStall.map((stall)=>console.log(stall.stallname));
*/
}
);
You are getting nothing because allStall is empty since you are not waiting for the promise to be fullfilled
try this
export const getAllStall = () => getDocs(collection(db, "hawkers"))
.then((querySnapshot) =>
querySnapshot.map((doc) =>
{
const {stallName, category, description} = doc.data();
return {
stallName:name,
stallCategory:category,
stallDescription:description
};
});
)
try to change use effect like this
useEffect(async () =>
{
const allStats = await getAllStall();
console.log(allStats)
allStats.forEach(console.log)
}
);
A very big thanks to R4ncid, you have been an inspiration!
And thank you all who commented below!
I managed to get it done with async and await. Latest update, I figure out what's wrong with my previous code too. I commented the solution in my question, which is adding the async to the function and await to getDocs.
Also map doesn't work in code above because field is stallname not stallName. However, it needs to be async + await if using in/calling from other function.
Helper function
export async function getAllStall(){
const querySnapshot = await getDocs(collection(db, "hawkers"));
var allStall = [];
querySnapshot.forEach(doc =>
{
var stall = doc.data();
var name = stall.stallname;
var category = stall.category;
var description = stall.description;
var stallData = {
stallName:name,
stallCategory:category,
stallDescription:description
};
allStall.push(stall);
}
);
return allStall;
}
Main JS file
useEffect(async () =>
{
const allStall = await getAllStall();
allStall.map((stall)=>console.log(stall.stallname));
}
);
Hurray

Nested forEach, both for pushing data in array, but the other one is showing 0 length in console

async function GetUserRecords(dbUID){
const dbUserRef = collection(db, "profile");
const dbUserResRef = collection(db, "profile", dbUID, "records");
const q = query(dbUserResRef, orderBy('date', 'desc'), limit(1));
onSnapshot (q, (querySnapshot2)=>{
// for (let doc2 of querySnapshot2){
querySnapshot2.forEach(doc2 =>{
const data1 = doc2.data();
usersRecords.push(doc2.data());
})
});}
async function GetAllDataRealtime(){
const dbUserRef = collection(db, "profile");
onSnapshot (dbUserRef, (querySnapshot)=>{
users = [];
usersRecords = [];
querySnapshot.forEach(doc =>{
var dbUID = doc.data().id;
GetUserRecords(dbUID);
users.push(doc.data());
});
AddAllItemsToTable(users, usersRecords)
})
window.onload = GetAllDataRealtime();
im using this code to get data in firestore and output it in a table, but when I run it, one of the two arrays will have a data inside but the length of it is 0, so it makes it never shows data in my table

Reading data from Firebase Array-Like

I have a model in Firebase, who is saved like this:
Im fetching the data in my componentDidMount like this:
fetchMatches = async () => {
const { firebaseApp, auth, pool } = this.props;
await firebaseApp.database()
.ref(`/pools/${pool.key}/users/${auth.uid}/`)
.once('value')
.then(snapshot =>{
this.setState({matches:snapshot.val()})
})
}
The problem is, that my state, becomes an object: Not an Array, not a list.
How can I read this data, in a proper way that I can filter it based on an atribute.
When I try to do
let matches = this.state.matches;
for (let index = 0; index < matches.length; index++) {
const element = matches[index];
console.log(element);
}
It not works.
When I try to use a this.state.matches.map() they say that is not a function. And it really isnt as on my debugger this.state.matches is an OBJECT.
What Im doing wrong here?
When you are fetching data with the once function it returns an object of that specific ref, in that case, it will return this ${auth.uid} data as an object.
So you might want to change your ref a little bit to: ref(/pools/${pool.key}/users/${auth.uid}/matches),
then to loop through the matches children, according to the firebase documentation https://firebase.google.com/docs/database/web/lists-of-data#listen_for_value_events
await firebaseApp.database()
.ref(`/pools/${pool.key}/users/${auth.uid}/matches`)
.once('value')
.then(snapshot =>{
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
// ...
});
})

firebase snapshot is not working with node js GCF

I have a firebase database structure in this format
the database is follows this format Ordergroup/groupid/item
I want to get the buyerid for each item once a new group is created in the ordergroup node. So I try this
exports.sendorderemailtoseller = functions.database.ref('/Ordergroup/{pushId}').onCreate((snapshot, context) => {
const parentRef = snapshot.ref.parent;
const ref = snapshot.ref;
const original = snapshot.val();
const buyerid = original.buyerid;
})
I then notice that original only returns the first child and the buyerid comes out as undefined. How can I get a snapshot of all the child in the groupid excluding Ordersummary?
Your variable 'original' is actually getting the whole tree under node 1522509953304, so you will need to iterate over its children to get each buyerid, like below:
exports.sendorderemailtoseller = functions.database.ref('/Ordergroup/{pushId}').onCreate((snapshot, context) => {
const buyerids = [];
snapshot.forEach((item) => {
buyerids.push(item.val().buyerid);
});
console.log(buyerids);
});

Categories