javascript - 如何将 firestore 'get' 中的数据加载到变量中?

标签 javascript angular google-cloud-firestore

我正在升级到 Angular v5 和 firestore,从 firestore 数据中提取字段时遇到很多问题。

我不想使用 angularfire2,因为 Angular 4 发布时我遇到了一些问题。

大约 10 天前,我花了很长时间研究如何从快照中获取数据。最后我得到了下面的代码来做到这一点。不幸的是“doc.data()['languageName'];”不适用于“获取”。

 querySnapshot.forEach(function (doc) {
    let name = doc.data()['languageName']; 
    lang.push(name);
  });
});

经过一整天的努力,我决定寻求帮助。我的最新尝试如下。

firebase.auth().signInWithEmailAndPassword(email, password)
  .then(staff => {
    if (staff.emailVerified) {
      this.result = this.db.collection("staff").doc(staff.uid).get()
       .then(function(staffDb) {                 // staff.uid works
         if (staffDb.exists) {      // <--  Works to here
           this.sid = staffDb.data.sid;          // nor staff.sid 
           this.staff2memoryService.set(staffDb.data());
           this.staff = this.db.collection("staff").
                                 doc(this.sid).get()
           .then(result => {
              if (this.sid) {
                this.staff2memoryService.set(result);
              } else {
                console.log('Not exist');
              }  

当我执行 console.log 时,我想要的所有数据都在那里,但我就是无法获取它。 firebase 文档显示了如何使用 console.log 访问字段,但这没有帮助,因为 console.log 似乎会自动提取/映射字段。

最佳答案

我昨天使用“this.data”尝试过,但没有成功,但以下有效。

    if (staffDb.exists) {  
      var data = staffDb.data();
      var eid = data.eid;

有时最好是在解决问题上 sleep ,昨天几个小时今天几分钟。我渐渐老了!

关于javascript - 如何将 firestore 'get' 中的数据加载到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904475/

相关文章:

javascript - 如何不在 IE8 或更早版本上执行脚本?

javascript - React-Native:错误:需要 JDK11 或更高版本。检测到不兼容的主要版本: '8'

angular - 如何从 Angular 11 降级到 Angular 9?

Angular 2 | 401未经授权

javascript - 仅包含集合的 Firestore 查询文档

listview - Flutter:使用来自 Firestore 的多集合流创建动态更新的 DataTable

javascript - 所有 HTML 文件中的通用 CSS

php - 发送/显示 base64 编码图像

html - Angular Material 2 : How to put a fab button on top of the md-table at the bottom right corner?

firebase - 如何修复 Firestore SDK 在 iOS 上遇到 "An internal error was encountered in the Indexed Database server"错误?