Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Is it possible to add a method dynamically in Javascript?

Hey I am making a firebase database call that looks like this:

db.collection("posts").where("location", "==", location)
    .get().then((querySnapshot) => {
        [...]
    })

The user can specify from what countries he would like to get posts (the .where() method is for that). But he can also pick all the countries, so in that case the method is no longer needed. Is there a way to add methods dynamically?

Something like this:

db.collection("posts")if(location != "all"){.where("location", "==", location)}
    .get().then((querySnapshot) => {
        [...]
    })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use a function

const addLocationCondition = (collection, location) => 
    location === "all" ? collection : collection.where('location', '==', location);

addLocationCondition(db.collection('posts'), location).get()...
about 4 years ago · Juan Pablo Isaza Report

0

No, you can't put a conditional there.

Just use if to decide whether to call .where().

let posts = db.collection("posts");
let filtered;
if (location == "all") {
    filtered = posts;
} else {
    filtered = posts.where("location", "==", location);
}
filtered.get().then(querySnapshot => { 
    // ...
});
about 4 years ago · Juan Pablo Isaza Report

0

I'm not completely sure about this db API but this should work:

let query = db.collection("posts")
if(location != "all"){
  query = query.where("location", "==", location)
}
query.get().then((querySnapshot) => {
   [...]
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!