如何从Firestore获取文档ID?

时间:2019-04-08 15:06:28

标签: typescript firebase firebase-authentication google-cloud-firestore ionic4

我正在尝试为管理员编写代码以更新存储在Cloud FireStore中的用户字段值,但无法获取文档ID和更新Cloud Firestore中的字段值。

我正在使用前端的ionic框架和后端的firebase来构建跨平台的移动应用,此应用只有一名管理员。管理员将控制用户访问应用程序的某些部分,因此,为此,我将不得不更新Firestore中用户文档中的字段值。

import { Component, OnInit } from '@angular/core';
import { User } from 'src/app/services/user/grade.service';
import { AdminService } from 'src/app/services/admin/admin.service';
import { Observable } from 'rxjs';
import { AngularFirestoreCollection, AngularFirestore } from 'angularfire2/firestore';
import {map} from 'rxjs/operators';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
export interface usrId extends User {
  id: string;

}
@Component({
  selector: 'app-student-details',
  templateUrl: './student-details.page.html',
  styleUrls: ['./student-details.page.scss'],
})
export class StudentDetailsPage implements OnInit {
public docId;
public userEmail;
public userName;
public data:any;
public user: User;
public userData:any;
public userStatus;
public visible: boolean;
public isDisabled: boolean; 
public isStatus: boolean;
public newStatus;
public users: Observable<User[]>;
private userProfileCollection: AngularFirestoreCollection<User>;
public uData: firebase.firestore.DocumentReference;
public currentUser: firebase.User;

    constructor(private adminService: AdminService, public db: AngularFirestore){
    this.userProfileCollection = this.db.collection<User>('userProfile');
    this.users = this.userProfileCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const udata = a.payload.doc.data() as User;
          const id = a.payload.doc.id;
          console.log(udata +"constructor Id");
          return { id, ...udata };
        });
      })
    );


    }

    getAllPosts(): Observable<any> {
      return this.db.collection<any>

(“ userProfile”)。valueChanges();         }

    runPost() {
      this.getAllPosts().subscribe((data)=>{
          this.data = data;
          this.user = JSON.parse(JSON.stringify(this.data[0]));
          console.log(this.user.id+""+"in run post");
      });
    }

  ngOnInit() {
    this.adminService
    .getUserDetails()
    .get()
    .then( userProfileSnapshot => {
      this.userData = userProfileSnapshot.data();
      console.log(this.userData.email)
      this.runPost();
    });
    this.visible = false;
    this.isDisabled = true;
    this.isStatus = true;
  }

  getStatus(){
    for(let data of this.data){
      if(data.email === this.userEmail){
        this.userStatus = data.status;
        this.userName = data.firstName;
        console.log

      }
    }
    this.visible = true;
  }

以下是一种更新方法,我正在使用该方法来更新用户字段值。由于这是为了授予用户帐户权限,因此我需要在所有文档中选择所需的文档并更新状态字段。 我正在尝试获取文档ID,以便可以更新相应的状态字段。 为了实现这一点,我在构造函数中使用了快照方法。但是我无法通过以下更新方法访问文档ID。

  updateStatus(us: User):Promise<void>{

    return this.userProfileCollection.doc(us.id).update({status:2});


  }

}





When the admin tries to update the field value, the update must successfully take place. And admin should be able to retrieve user docId.

0 个答案:

没有答案