仅当通过python firebase SDK添加文档时,angularfire上的空文档才会更新

仅当通过python firebase SDK添加文档时,angularfire上的空文档才会更新,python,google-cloud-firestore,Python,Google Cloud Firestore,我对firestore和angular绝对是新手,但经过几天的工作,我已经成功地编写了一个ionic/angular web应用程序,它使用google firestore作为后端 每次在firestore中添加/删除/更新条目时,该应用程序都会显示实时更新 我通过订阅valueChanges来响应数据库中的更改: itemSubscription; 私人物品收集:AngularFirestoreCollection; 项目:可观察; 建造商(私人afs:AngularFirestore){ t

我对firestore和angular绝对是新手,但经过几天的工作,我已经成功地编写了一个ionic/angular web应用程序,它使用google firestore作为后端

每次在firestore中添加/删除/更新条目时,该应用程序都会显示实时更新

我通过订阅valueChanges来响应数据库中的更改:

itemSubscription;
私人物品收集:AngularFirestoreCollection;
项目:可观察;
建造商(私人afs:AngularFirestore){
this.itemsCollection=afs.collection('zones/Zone1/trashdumps');
this.items=this.itemsCollection.valueChanges();
//subscribe()用于获取更新流。
this.itemSubscription=this.items.subscription(
//在每个新集合快照上调用的函数
(文件)=>{
docs.forEach(doc=>{
console.log('当前数据:',文档);
})
},
//错误时调用的函数
(msg)=>{console.log('获取位置时出错:',msg);}
);
我的数据库的典型条目如下所示

{
“包”:
{
“integerValue”:“2”
},
“地点”:
{
“地质点值”:
{
“纬度”:52.3696,
“经度”:4.86561111
}
}
}
当我通过google firestore web控制台手动向firestore添加条目时,我的应用程序会收到正确的更新

当我通过RESTAPI向firestore添加条目时,我的应用程序会收到正确的更新

当我使用python的firestore管理模块向firestore添加条目时,我的应用程序收到一个空的更新。注意:只有在条目创建时收到的实时更新是空的!如果我只是重新加载我的应用程序并再次查询db,我会得到一个正确的条目!python编写的db条目从各个角度来看都是正确的

以下是我所说的“正确更新”(取自chrome控制台)的意思:

这是“emtpy”,错误的更新:

tab2.page.ts:50 Current data: Object
                              __proto__: Object
我已经尝试订阅snapshotChanges()而不是valueChanges()。元数据确实存在,但是没有.data()方法来获取字段.Empty

以下是我如何在Python中向firestore添加条目(简化):


类MyPoint(对象):
定义初始(自身、行李、横向、纵向):
self.bags=行李
self.location=地质点(纬度、经度)
@静力学方法
来自_dict的def(来源):
# ...
通过
def到dict(自身):
# ...
返回{
“bags”:self.bags,
“位置”:self.location
}
类MyDatabase(对象):
_firebaseConfig={
[...]
}
定义初始化(自):
#使用应用程序默认凭据
cred=credentials.ApplicationDefault()
firebase\u管理员初始化\u应用程序(cred,self.\u firebaseConfig)
self.db=firestore.client()
def发送(自身,点):
self.db.collection(u'mycollection').add(point.to_dict())
点=我的点(1,0.0,0.0)
db=MyDatabase()
数据库发送(点)
我做错什么了吗?firestore管理员是否可以分两步执行条目创建,首先创建一个空记录,然后设置它的值?这就可以解释了吗

我希望这是清楚的,谢谢你的帮助,真的是一个新手

更新 我更准确地记录了当我订阅DocumentChangeAction观测时会发生什么,我甚至更困惑

在下面的日志中,“Doc”表示DocumentChangeAction,“data”表示实际的.payload.Doc.data()文档体

从空数据库开始,这是我添加第一个点时收到的更新:

tab2.page.ts:49 Current Doc: {type: "added", payload: {…}}
tab2.page.ts:50 Current data: {}
tab2.page.ts:49 Current Doc: {type: "modified", payload: {…}}
tab2.page.ts:50 Current data: {bags: 1, location: GeoPoint}

事实上,一个空的“添加”和一个后续的“修改”。我可以处理这个问题

但这是我在添加第二点(即“bags=8”点)时得到的结果:

这太多了:4次更新

  • 由于某种原因修改了上一个条目。可能是元数据,而不是字段
  • 执行空添加
  • 再次发送上一个条目(?)
  • 最后,发送新条目的更新以及数据

  • 是我还是这真的太复杂了?

    我测试了相同的,结果是,将返回每个更改的所有对象的列表。您应该寻找一个替代方法,例如只提供最新更改

        this.items = db.collection('mytesting').valueChanges();
    
        this.items.subscribe(
          //function invoked on every new collection snapshot
          (docs) => {
            docs.forEach( doc => {
              console.log('Current data:', doc);
            })
          },
          //function invoked on error
          (msg) => { console.log('Error Getting Location: ', msg); }
        );
    
    打印控制台日志时的所有项目:

    app.component.ts:21 Current data: {name: "nagaraj"}
    app.component.ts:21 Current data: {name: "Sample"}
    app.component.ts:21 Current data: {name: "Test"}
    app.component.ts:21 Current data: {name: "Fetch"}
    
    我添加了一个新项目,比如
    {name:“other”}
    ,它会打印出来

    app.component.ts:21 Current data: {name: "nagaraj"}
    app.component.ts:21 Current data: {name: "Sample"}
    app.component.ts:21 Current data: {name: "Test"}
    app.component.ts:21 Current data: {name: "Fetch"}
    app.component.ts:21 Current data: {name: "Another"}
    
    如果我将行更改为:

    this.items = db.collection('mytesting').stateChanges();
    
    它不会打印所有项目,只有状态已更改为添加、修改和删除的文档

    然而,我使用了AngularFirestore,我可以在UI中显示相同的内容,而无需订阅
    valueChanges()。订阅(

    应用程序组件.ts

    import { Component } from '@angular/core';
    import { AngularFirestore } from '@angular/fire/firestore';
    import { Observable } from 'rxjs';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular';  
      items: Observable<any[]>;
    
      constructor(db: AngularFirestore) {
        this.items = db.collection('mytesting').valueChanges();
      }
    }
    
    从'@angular/core'导入{Component};
    从'@angular/fire/firestore'导入{AngularFirestore};
    从“rxjs”导入{Observable};
    @组成部分({
    选择器:“我的应用程序”,
    templateUrl:“./app.component.html”,
    样式URL:['./app.component.css']
    })
    导出类AppComponent{
    名称='角度';
    项目:可观察;
    建造商(db:AngularFirestore){
    this.items=db.collection('mytesting').valueChanges();
    }
    }
    
    app.component.html

    <ul>
      <li class="text" *ngFor="let item of items | async">
        {{item.name}}
      </li>
    </ul>
    
    • {{item.name}

    我没有使用
    subscribe

    我猜你订阅了一个错误的操作基础API,因为他们的文档将返回每个更改的所有对象的列表。你应该寻找一个替代方法,比如只提供最新的更改。我没有时间测试它,因此在这里发表评论。
    import { Component } from '@angular/core';
    import { AngularFirestore } from '@angular/fire/firestore';
    import { Observable } from 'rxjs';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular';  
      items: Observable<any[]>;
    
      constructor(db: AngularFirestore) {
        this.items = db.collection('mytesting').valueChanges();
      }
    }
    
    <ul>
      <li class="text" *ngFor="let item of items | async">
        {{item.name}}
      </li>
    </ul>