I am making a web app with firebase as my database, but I have run into a problem while programming in javascript. I have two different sets of collections with information in them. In order to avoid duplication, I need to combine them to get data from them. To do that, I went ahead and created IDs in one of the collections to relate it to the other.
Here is some code that I have tried from another stackoverflow Link to that forum
db.collection("articlesCollection")
.doc(articleId)
.get()
.then(article => {
firebase.firestore().collection("commentsCollection")
.where('articleRef.' + article.id, '==', true)
.get()
.then(results => {
results.forEach(function (comment) {
console.log(comment.id, " => ", comment.data());
});
});
});
This is code the code from above that I tried to repurpose but ended up stopping after not quite understanding how its structure worked.
db.collection("Users").get().then(article => {
firebase.firestore().collection("trips").where('Users.' + doc.id, '==', ownersID)
.get()
.then(results => {
//got lost here. I need the forEach function, but
results.forEach(function (comment) {
console.log(comment.id, " => ", comment.data());
//eventually change these into variables to display in a
//form.
});
});
});
Below will be a brief rundown on the structure of my database. There will also be information that I will use () to describe what is happening.
Trips\uniqueID(generated by Google)\ownerID(& other info)
Users\ownerID\info.
As you can see, the ownerID is found in two sets of collections. I need information from the trips collection on where they are going. I need information from the Users collection because it has to owners information such as name and email.
After doing research and trial and error here is what I found that may help with this.
Here is what I had before. Hopefully, it offers some more insight on what I am trying to do with my javascript.
db.collection("trips").where("flag", "==", true)
.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
//this info needs to be taken from the Users collection
var fiName = doc.data().fName;
var laName = doc.data().lName;
var fullName = fiName + " " + laName;
var wscEmail = doc.data().wscEmail;
var phoneNum = doc.data().phoneNumber;
//Information that I can take from the trips table.
var id = doc.data().ownerID;
var idName = doc.id;
var destination = doc.data().destination;
var location = doc.data().location;
var date = doc.data().dateGoing;
var arrivalTime = doc.data().arrivalTime;
var departureTime = doc.data().departureTime;
var returnTime = doc.data().returnTime;
var seats = doc.data().seats;
//leave this for now. Gopal is working on it
//var email = "temp@Email";
//This is something we can do, but we can also leave it
//var phoneNum = "(712) 111-2211";
var description = doc.data().tripDescription;
feed.innerHTML += " \
<div'>\
<div class='post'>\
<div>\
<img src='circleProfile.png' alt='User photo'>\
<div class='name'> <p>" + fullName + "</p></div>\
<div>Destination: <span class='postInfo'>" + destination + "</span></div>\
<div>Location: <span class='postInfo'>" + location + "</span></div>\
<div>Date: <span class='postInfo'>" + date + "</span></div>\
<table>\
<tr>\
<th>Departure</th>\
<th>Arrival</th>\
<th>Return</th>\
<th>Seats</th>\
</tr>\
<tr>\
<td>" + departureTime + "</td>\
<td>" + arrivalTime + "</td>\
<td>" + returnTime + "</td>\
<td>" + seats + "</td>\
</tr>\
</table>\
<div>\
<p>Email: " + wscEmail + " <span class='postInfo'>" + phoneNum + "</span></p>\
</div>\
<div class='description'><p>" + description + "</p></div>\
<div class='spaceWars' onclick='deletePost(this.id)' id=" + idName + "> <button> Click me</button> </div>\
</div>\
</div>\
</div>";
});
});
});
As you have seen, there is information that I need from another collection to be in this table. The problem is I do not know how to incorporate the other collection.
Side Note: This is my first time working with a non-relational database, so the formating and structure behind it has been throwing me off. There is also the fact that there are some things that firebase does not have functionality for yet which makes doing this a little harder.
Thank you for reading, I know it was a lot and if you make it through all of it and understand what I am doing, I greatly appreciate it. Hopefully this will offer some insights to others in the future.