0

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.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
potato
  • 97
  • 5
  • 1
    Sounds like a clear use-case. Are you asking [how to get a parameter from the URL in JavaScript](https://stackoverflow.com/questions/979975/get-the-values-from-the-get-parameters-javascript)? Or [how to run a query on Firestore](https://firebase.google.com/docs/firestore/query-data/queries)? – Frank van Puffelen Jan 30 '22 at 15:30

1 Answers1

2

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);
Tim
  • 2,843
  • 13
  • 25