问题
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..
回答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.
回答2:
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'))
})
来源:https://stackoverflow.com/questions/59580013/how-to-get-map-of-firebase-cloud-store-data-in-node-js