简体   繁体   English

Firebase 文档删除奇数段

[英]Firebase document deletion odd number of segments

I'm trying to delete an entry from firebase upon clicking a button in my table, but i get this error:我试图通过单击表中的按钮从 firebase 中删除一个条目,但出现此错误:

FirebaseError: Invalid document reference. FirebaseError:无效的文档引用。 Document references must > have an even number of segments, but iEwblJo832nnC6TkFDEy has 1.文档引用必须 > 有偶数个段,但 iEwblJo832nnC6TkFDEy 有 1 个。

I don't understand why the document has an odd number of segments since I just queried it.我不明白为什么文档的段数是奇数,因为我只是查询它。 How can i fix this?我怎样才能解决这个问题?

The function which does the query select and attempts deletion查询 select 并尝试删除的 function

async deleteMovie(movieToDelete : Movie) {
    await this.moviesRef.ref.where('includedBy', '==', movieToDelete.includedBy).where('title', '==', movieToDelete.title).get().then((querySnapshot) => {
      querySnapshot.forEach((doc) => this.database.doc(doc.id).delete());
    });
    return true;
  }

The click function of the delete button删除按钮的点击 function

async deleteEntry (item : Movie) {
    let idx = this.movieData.data.indexOf(item);
    if(await this.movieService.deleteMovie(this.movieData.data[idx]) === true) {
      this.movieData.data.splice(idx,1);
      this.movieData._updateChangeSubscription();
    }
    else {
      window.location.reload();
    }
  }

The html page where the button is created创建按钮的html页面

<div class="container-page">
    <p id="toolbar-top">
        <mat-toolbar color="primary">
            Welcome (de pus numele userului aici)
            <button mat-icon-button id="logout-icon">
                <mat-icon>logout</mat-icon> Logout
            </button>
        </mat-toolbar>
    </p>
    <table class="mat-elevation-z8 table-main" mat-table [dataSource]="movieData" matSort>
        <ng-container matColumnDef="title">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Title </th>
            <td mat-cell *matCellDef="let element"> {{element.title}} </td>
        </ng-container>
        <ng-container matColumnDef="genre">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Genre </th>
            <td mat-cell *matCellDef="let element"> {{element.genre}} </td>
        </ng-container>
        <ng-container matColumnDef="watchStatus">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Status </th>
            <td mat-cell *matCellDef="let element"> {{element.watchStatus}} </td>
        </ng-container>
        <ng-container matColumnDef="rating">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Rating </th>
            <td mat-cell *matCellDef="let element"> {{element.rating}} </td>
        </ng-container>
        <ng-container matColumnDef="wouldRecommend">
            <th mat-header-cell *matHeaderCellDef mat-sort-header> Recommended ? </th>
            <td mat-cell *matCellDef="let element"> {{element.wouldRecommend}} </td>
        </ng-container>
        <ng-container matColumnDef="actions">
            <th mat-header-cell  *matHeaderCellDef ></th>
            <td mat-cell *matCellDef="let row" >
              <button mat-raised-button color="warn" (click)="deleteEntry(row)">Delete</button> 
            </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

    <mat-paginator class="paginator-main" [pageSizeOptions]="[5, 10, 15]"
                 showFirstLastButtons 
                 aria-label="Select page of movies list">
    </mat-paginator>

    <button id="new-entry-button" mat-raised-button color="primary" (click)="addNewEntry()">Add new movie</button>
</div>

The movie interface电影界面

export interface Movie {
    includedBy : string;
    title : string;
    genre : string;
    watchStatus : string;
    rating : string;
    wouldRecommend : string;
}

This is how my firebase collection looks like这就是我的 firebase 系列的样子我的 Firebase 系列看起来如何

You're querying this.moviesRef , but then are deleting from this.database , so it seems those two references are different.您正在查询this.moviesRef ,但随后正在从this.database中删除,因此这两个引用似乎不同。

To delete from a query, you can just get the reference of the document snapshot in the result itself:要从查询中删除,您只需在结果本身中获取文档快照的引用:

this.moviesRef.ref
  .where('includedBy', '==', movieToDelete.includedBy)
  .where('title', '==', movieToDelete.title)
  .get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => doc.ref.delete());
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合引用必须有奇数个段 - Collection references must have an odd number of segments 集合引用无效。 集合引用必须有奇数个段 - Invalid collection reference. Collection references must have an odd number of segments 文档引用在集合引用上必须有偶数段错误 - Document references must have an even number of segments error on a collection reference 未捕获的 FirebaseError:无效的文档引用。 文档引用必须有偶数个段,但 todos 有 1 - Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1 Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but<x> 有 1,空)</x> - Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but <x> has 1, null) 如何限制firebase中一个文件里面创建的collections个数? - How to limit the number of collections created inside a document in firebase? 如何刷新 firebase streambuilder 并减少文档读取次数 - How to refresh a firebase streambuilder and reduce the number of document reads Twilio:未捕获的错误:段数错误 - Twilio: Uncaught Error: Wrong number of segments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM