2021年2月3日星期三

trying to update field in cloud firestore

I'm very new to Firebase/Firestore.

I created a function modifyRole that takes two parameters, uid and role, and should update my database by changing the role, which the user selects from a dropdown.

But I'm getting an error, as follows:

ERROR Error: Uncaught (in promise): FirebaseError: [code=not-found]: No document to update: projects/headstart-imm/databases/(default)/documents/users/role

Error: No document to update: projects/headstart-imm/databases/(default)/documents/users/role.

Here is my code:

import { animate, state, style, transition, trigger } from '@angular/animations';  import { ChangeDetectionStrategy, Component, OnInit, ViewEncapsulation } from '@angular/core';  import { AngularFirestore } from '@angular/fire/firestore';  import { tap } from 'rxjs/operators';  import { fuseAnimations } from '../../../@fuse/animations';    @Component({    selector: 'app-users',    templateUrl: './users.component.html',    styleUrls: ['./users.component.scss'],    changeDetection: ChangeDetectionStrategy.OnPush,    encapsulation: ViewEncapsulation.None,    animations: [      fuseAnimations,      trigger('detailExpand', [        state('void', style({ height: '0px', minHeight: '0', visibility: 'hidden' })),        state('*', style({ height: '*', visibility: 'visible' })),        transition('void <=> *', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),      ]),    ],  })  export class UsersComponent implements OnInit {    userColumns = ['photoURL', 'name', 'email', 'providerId', 'role'];      users$ = this.afs.collection<Users[]>('users').valueChanges().pipe(tap(console.log));      constructor(private afs: AngularFirestore) {}      ngOnInit(): void {}      modifyRole(uid: string | any, role: string) {      return this.afs.collection('users').doc('role').update({        role,        uid,      });    }  }    export interface Users {    displayName: string;    email: string;    phoneNumber: null;    photoURL: string;    providerId: string;    role: string;    uid: string;  }  

this is the template

<mat-table #table *ngIf="users$ | async as users" [dataSource]="users" multiTemplateDataRows=true>    < id="users-project" class="page-layout simple right-sidebar" fxLayout="row" fxLayoutAlign="space-around center">      <ng-container matColumnDef="photoURL">        <mat-header-cell *matHeaderCellDef></mat-header-cell>        <mat-cell *matCellDef="let user"><div *ngIf="user?.photoURL">          <img alt="profilePic" src=""></div></mat-cell>      </ng-container>      <ng-container matColumnDef="name">        <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>        <mat-cell *matCellDef="let user"></mat-cell>      </ng-container>      <ng-container matColumnDef="email">        <mat-header-cell *matHeaderCellDef>Email Address</mat-header-cell>        <mat-cell *matCellDef="let user"></mat-cell>      </ng-container>      <ng-container matColumnDef="providerId">        <mat-header-cell *matHeaderCellDef>Provider</mat-header-cell>        <mat-cell *matCellDef="let user"></mat-cell>      </ng-container>  <!--    <ng-container matColumnDef="role">-->  <!--      <mat-header-cell *matHeaderCellDef>Role</mat-header-cell>-->  <!--      <mat-cell *matCellDef="let user"><p></p></mat-cell>-->  <!--    </ng-container>-->      <ng-container matColumnDef="role">        <mat-header-cell *matHeaderCellDef>Role</mat-header-cell>          <mat-cell *matCellDef="let user">            <mat-form-field>              <mat-select (selectionChange)="modifyRole(user.uid, user.role)" name="modifyRole">                <mat-option *ngFor="let user of users"                        [value]=user.role></mat-option>              </mat-select>            </mat-form-field>          </mat-cell>      </ng-container>      <ng-template #tpl let-user>        <div class="mat-row detail-row" [@detailExpand] style="overflow: hidden"></div>      </ng-template>      <mat-header-row *matHeaderRowDef="userColumns; sticky: true"></mat-header-row>      <mat-row *matRowDef="let row; columns: userColumns;" matRipple class="element-row"               [cdkDetailRow]="row" [cdkDetailRowTpl]="tpl"></mat-row>  </mat-table>  

enter image description here

https://stackoverflow.com/questions/66037260/trying-to-update-field-in-cloud-firestore February 04, 2021 at 07:29AM

没有评论:

发表评论