【问题标题】:firestore loading data in array javascriptFirestore在数组javascript中加载数据
【发布时间】:2021-04-04 09:53:10
【问题描述】:

无法将 Firestore 数据加载到数组中。

我的代码是:

db.collection("vertrag").get().then((querySnapShot) => {
  array = querySnapShot.docs.map((doc) => ({ id: doc.id, ...doc.data(), }));
  console.log(array);
});

效果很好

问题 - 目前我尝试在 {} 之外使用我的数组,它返回未定义。

尝试了不同的赋值(let、const、通过函数返回),但没有改善 - 一旦在 {} 之外我无法使用我的数组

请有人帮助我 - 这只能是一个小话题,因为命令本身可以正常工作!

【问题讨论】:

  • 试试const array =
  • 数组在哪里定义?即让数组; db.collection("vertrag").get()...
  • 在代码开头用 let
  • 在其他场合发现,由于 set()-command 的时间滞后,也会发生此类问题。意味着 - 程序更快地执行下面的代码并“接管” set() 命令,这会导致未定义,因为数组仍在等待填充...
  • 用const也试过了,和let一样,没区别

标签: javascript arrays google-cloud-firestore


【解决方案1】:
Where is array defined? i.e.
let array;
db.collection("vertrag").get().then((querySnapShot) => {
  array = querySnapShot.docs.map((doc) => ({ id: doc.id, ...doc.data(), }));
  console.log(array);
});

// Now array will be available outside the {} closure.  However, the promise 
// is async.  So, you'll need to account for that.  If this is a react 
// project, for example, you would set the array on state, to trigger 
// any code that depends on the array.

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2021-04-22
    • 2019-04-25
    • 2021-05-01
    • 2018-11-17
    • 1970-01-01
    • 2011-03-18
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多