1

Actually I m making a discord bot list ,I want to show all the bots ,I m using express nodejs. My data in firebase are as shown below

db.collection("bots").doc("12345").set({
prefix:"?",
id:"12345",
server:"5"
})
db.collection("bots").doc("12346").set({
prefix:"-",
id:"12346",
server:"7"
});


const x = require ("express");
const router = x.Router ()
const {db} = require("../app");
  db.collection("bots").
get().then((querySnapshot) => { const x = require ("express");
const router = x.Router ()
const {db} = require("../app");
  db.collection("bots").where("approved", "==", false).
get().then((querySnapshot) => { 
    querySnapshot.forEach(function(doc) {
   const bot =  doc.data()
   console.log(bot. prefix)
      router.get("/",(req,res) => {
        res.send(bot.id+"=>"+bot.prefix)
                 })
    });
})

module.exports = router;

Output:

12345 => "?"

__ Expected output

12345 => "?"
12346 => "-"

When I console,it returns both prefix But when I tried to render it ,it showing only the first one..

2 Answers 2

1

If you're trying to show a list of documents, follow the recipe shown in the documentation on getting all documents from a collection:

db.collection("bots").get().then((querySnapshot) => { 
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
})

I highly recommend studying that, and the rest of, the documentation, as it has code samples for many common use-cases such as this one.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to send result after you looped through snapshot. Try something like this:

let results = []
querySnapshot.forEach(function(doc) {
    const bot =  doc.data()
    console.log(bot. prefix)
    results.push(bot.id+"=>"+bot.prefix)
});
router.get("/",(req,res) => {
    res.send(results.join('\n'))
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.