简体   繁体   中英

Is there a way to create a Firestore Document using currentUser uid

I am trying to use Angular and Firebase to set up a frontend back end relationship in the simplest way. i need when a user is logged in that the users information gets pushed to Firestore. I get the token pushed but the users data stops the pipeline and nothing gets pushed.

" firebase.auth().currentUser " to add data to a Firestore collection, but everytime i try the app seems to not push any data.

 import { Component } from '@angular/core'; import { AngularFireMessaging } from '@angular/fire/messaging'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/take'; import { filter } from 'rxjs/operators'; import { mergeMapTo } from 'rxjs/operators'; import { mergeMap } from 'rxjs/operators'; import { AngularFireAuth } from '@angular/fire/auth'; import firebase from 'firebase/app'; import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore'; import { Observable } from 'rxjs'; import { switchMap } from 'rxjs/operators'; export class AppComponent { private user = firebase.auth().currentUser constructor(private afMessaging: AngularFireMessaging, public auth: AngularFireAuth, private afs: AngularFirestore) {} requestPermission() { this.afMessaging.requestPermission.pipe(mergeMapTo(this.afMessaging.tokenChanges)).subscribe( (token) => { this.afs.collection('users').add({ fcmtoken: token, userid: user.uid }) }, (error) => { console.error(error); }, ); } listen() { this.afMessaging.messages.subscribe((message) => { console.log(message); }); } deleteToken() { this.afMessaging.getToken.pipe(mergeMap(token => this.afMessaging.deleteToken(token))).subscribe( (token) => { console.log('Token Has Been Deleted, Get a New One'); }, ); } login() { this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()); } logout() { this.auth.signOut(); } }

The problem comes in here

 requestPermission() { this.afMessaging.requestPermission.pipe(mergeMapTo(this.afMessaging.tokenChanges)).subscribe( (token) => { this.afs.collection('users').add({ fcmtoken: token, //if i remove this - all works well. the token is pushed// userid: user.uid // }) }, (error) => { console.error(error); }, ); }

How do i take this code and create a system where a new document is created for every user and that document holds the used data, including the tokens.

maybe... user is invalid, your code doesn't work.

requestPermission(user: User) { // (1) give a valid user.
  this.afMessaging.requestPermission
    .pipe(mergeMapTo(this.afMessaging.tokenChanges))
    .subscribe(
      (token: string | null) => { // (2) this can be null.
        if (!token) { // (3) add validation.
          return
        }
        this.afs.collection('users').add({
          fcmtoken: token,

          // (4) this will work.
          userid: user.uid

          //
        })
      },
      (error: any) => {
        console.error(error);
      },
    );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM