Search code examples
google-cloud-firestoreangularfire

How do I use doc and firestore with @angular/fire/firestore to access the database and insert a record


I have been attempting to use the following code to add a user to firestore and then add a user record in the database.
I have been able to add a user and receive the proper returns, but on

doc(this.firestore, `users/${credentials.user.uid}`);

I receive the error

fail with FirebaseError: [code=invalid-argument]: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore.

import { Injectable } from '@angular/core';
import {Auth,createUserWithEmailAndPassword,sendPasswordResetEmail,signInWithEmailAndPassword,     signOut} from '@angular/fire/auth';
import { doc, Firestore } from '@angular/fire/firestore';
import { setDoc } from '@firebase/firestore';

constructor(private auth: Auth, private firestore: Firestore) { 
}


async register({ email, password }: { email: string; password: string }) {
  try {
    const credentials = await createUserWithEmailAndPassword(
      this.auth,
      email,
      password
    ); // returns uid and access tokens etc..
  
    const ref = doc(this.firestore, `users/${credentials.user.uid}`);  // get a document reference
    // fail with FirebaseError: [code=invalid-argument]: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

    setDoc(ref, { email });// set to referenced document 
    return credentials;    
  } catch (e) {
     return null;    
   }

}

I have tried to copy exactly from the docs:

https://github.com/angular/angularfire

 import { doc, collection } from '@angular/fire/firestore'; // doc collection fail, it seems and get or insert into the database fails

and still I receive the same error. Does anyone have any experience using angularfire 7.6.1 with ionic 7 and capacitor 5, or know of an example of referencing a Firebase document with latest ionic versions


Solution

  • The problem is in node modules /angularFire v7.6.1 @firebase / rxfire / CountSnapshot exports and imports. I commented some of them out depending on firestore function and it worked. The is not an ideal solution as new errors are sure to follow. The only course of action I see is waiting for the new AngularFire release as they are aware of this problem and are working on a solution.