【问题标题】:Getting data from Firebase with date range in Angular 2从 Angular 2 中的日期范围的 Firebase 获取数据
【发布时间】:2019-11-20 09:07:32
【问题描述】:

我有一个这样的 Firebase 集合:

我正在尝试使用此代码获取具有日期范围条件的数据:

ticket-form.component.ts

onSubmmitDates(value: any) {
    debugger;
    this.ts.getSpecificTicket(value.dateFrom, value.dateTo).subscribe((data: any) =>{
      console.log(data);
    });
}

ticket.service.ts

private ticketList: AngularFireList<any>;

constructor(
    private firestore: AngularFirestore,
    private afDatabase: AngularFireDatabase,
) { }
getSpecificTicket(dateFrom: Date, dateTo: Date): any {
    this.ticketList = this.afDatabase.list('tickets', ref =>
    ref.orderByChild('issuedOn').startAt(dateFrom.toString()).endAt(dateTo.toString()));
    return this.ticketList.snapshotChanges();
}

当我运行它时,我得到了这个错误:

错误错误:permission_denied at /tickets:客户端没有 访问所需数据的权限。 在 errorForServerCode (index.cjs.js:642) 在 onComplete (index.cjs.js:8944) 在 Object.onComplete (index.cjs.js:12467) 在 index.cjs.js:11611 在 PersistentConnection.push../node_modules/@firebase/database/dist/index.cjs.js.PersistentConnection.onDataMessage_ (index.cjs.js:11853) 在 Connection.push../node_modules/@firebase/database/dist/index.cjs.js.Connection.onDataMessage_ (index.cjs.js:11170) 在 Connection.push../node_modules/@firebase/database/dist/index.cjs.js.Connection.onPrimaryMessageReceived_ (index.cjs.js:11164) 在 WebSocketConnection.onMessage (index.cjs.js:11065) 在 WebSocketConnection.push../node_modules/@firebase/database/dist/index.cjs.js.WebSocketConnection.appendFrame_ (index.cjs.js:10657) 在 WebSocketConnection.push../node_modules/@firebase/database/dist/index.cjs.js.WebSocketConnection.handleIncomingFrame (index.cjs.js:10707)

这是我的规则:

我能做些什么来解决这个问题?

【问题讨论】:

标签: angular firebase


【解决方案1】:

确保您已授予读/写权限,如图所示。

【讨论】:

  • 谢谢。它解决了我的问题,但现在我有一个不同的问题。它无法从数据库中获取数据。我得到 0 个计数。
  • 您可以尝试从数据库中获取所有数据,然后通过查询特定数据。希望它能帮助你摆脱问题
【解决方案2】:

检查您对数据库 Firebase 的权限。如果您处于开发模式,您可以删除权限,但任何人都可以访问。暂时你在开发,访问权限没有问题的时候,你可以简单的如下图做。

service cloud.firestore {
  match /databases/{database}/documents {
  match /{document=**} {
   allow read, write;
  }
 }
}

在规则部分。

【讨论】:

  • 谢谢。它解决了我的问题,但现在我有一个不同的问题。它无法从数据库中获取数据。我得到 0 个计数。
  • 我猜您正在使用实时数据库中的文档,并且您正在使用 firestore 数据库。您必须使用collection 而不是list。考虑阅读文档以获得更好的理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2022-01-15
  • 2020-08-08
相关资源
最近更新 更多