如何在Node.js中获取Firebase Cloud Firestore数据的地图?

huangapple go评论74阅读模式
英文:

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'))
})

huangapple
  • 本文由 发表于 2020年1月3日 22:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定