Как получить идентификатор документа, который я запросил? - PullRequest
0 голосов
/ 19 сентября 2018

Моя цель - позволить учащемуся зарегистрировать свой аккаунт в fb / google, введя ему код.

Как я структурировал данные

registration-code{
  docId: {
    code:'52400028'
  }
}

provider.ts

  checkIfRegistrationCodeExists(userTypedCode:string){
    this.registrationCodeCollectionRef = this.afDb.collection('registration-code', 
    ref => ref.where('code', '==', userTypedCode));
    this.registrationCodeCollection = this.registrationCodeCollectionRef.valueChanges();
  }

Затем я проверяю, соответствует ли оно

  checkIfRegistrationCodeExists(){
    console.log(this.userTypedCode);

registrationCode.ts

this.registrationCodeProvider.checkIfRegistrationCodeExists(this.userTypedCode);
this.registrationCodeProvider.registrationCodeCollection.subscribe(code => {
      if(code.length = 1){
        console.log('Matched')
      } else {
        console.log('No matches');
      }
    })
  }

Теперь моя проблема в том, как мне получить идентификатор документа для документав вопросе?

Какие-нибудь советы, как это сделать?Спасибо!

1 Ответ

0 голосов
/ 19 сентября 2018

Глупый я, хе-хе.Мне нужно только использовать метод snapshotChanges для этого, так как я все еще получаю коллекцию.

  checkIfRegistrationCodeExists(userTypedCode:string){
    this.registrationCodeCollectionRef = this.afDb.collection('registration-code', 
    ref => ref.where('code', '==', userTypedCode));
    this.registrationCodeCollection = this.registrationCodeCollectionRef.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as RegistrationCode;
        const id = a.payload.doc.id;
        return { id, ...data };
      })), 
    );
    return this.registrationCodeCollection;
  }

Затем я делаю это, чтобы получить идентификатор

  checkIfRegistrationCodeExists(){
    console.log(this.userTypedCode);
    this.registrationCodeProvider.checkIfRegistrationCodeExists(this.userTypedCode);
    this.registrationCodeProvider.registrationCodeCollection.subscribe(code => {
      if(code.length = 1){
        console.log('Matched', code[0].id);
      } else {
        console.log('No matches');
      }
    })
  }
...