英文:
How to get map of firebase cloud store data in node js?
问题
Output:
12345 => "?"
Expected output:
12345 => "?"
12346 => "-"
When I console, it returns both prefix
. But when I tried to render it, it's showing only the first one.
英文:
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
得分: 1
如果您想显示文档列表,请按照文档中提供的示例操作:从集合中获取所有文档。
db.collection("bots").get().then((querySnapshot) => {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
我强烈建议您学习该文档以及其他文档,因为它包含了许多常见用例的代码示例,例如这个示例。
英文:
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
得分: 0
我认为你需要在遍历快照后发送结果。尝试类似以下的代码:
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'))
})
英文:
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'))
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论