As in the title, I want to show only documents with matching fields in Firebase.
Now my code using jquery looks like this:
const db = firebase.firestore();
db.collection('product').get().then((product)=>{
product.forEach((doc)=>{
var li = "";
console.log(doc.data())
if(topic == doc.data().topic){
li = `
<div>
<img class="${img}" src="${doc.data().img}">
<p class="text-500 text-white">${doc.data().title}</p>
</div>
`
}else{
li = `
<div>
<img class="${img}" src="${doc.data().img}">
<p class="text-500 text-white">${doc.data().title}</p>
</div>
`
}
$(`.app`).append(li)
})
});
<div class="app"></div>
All documents are now available. For example, if the current URL is "https://example.com?topic=firebase", I want to display only the contents of the documents with the topic field firebase in the documents in a collection called product.
Thank you in advance.
To get the value of the URL parameter "topic", try this:
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
const topic = getParameterByName("topic", window.location.href);