Invalid document reference error

I have this error:

core.js:1440 ERROR Error: Uncaught (in promise): FirebaseError:     
[code=invalid-argument]: Invalid document reference. 
Document references must have an even number of segments, but users has 1

FirebaseError: Invalid document reference. 
Document references must have an even number of segments, but users has 1

at new FirestoreError (error.js:149)
at Function.DocumentReference.forPath (database.js:436)
at Firestore.doc (database.js:245)
at AngularFirestore.doc (firestore.js:28)
at new Firebase (firebase.service.ts:25)
at _createClass (core.js:10877)
at _createProviderInstance$1 (core.js:10847)
at resolveNgModuleDep (core.js:10832)
at NgModuleRef_.get (core.js:12069)
at resolveDep (core.js:12559)
at new FirestoreError (error.js:149)
at Function.DocumentReference.forPath (database.js:436)
at Firestore.doc (database.js:245)
at AngularFirestore.doc (firestore.js:28)
at new Firebase (firebase.service.ts:25)
at _createClass (core.js:10877)
at _createProviderInstance$1 (core.js:10847)
at resolveNgModuleDep (core.js:10832)
at NgModuleRef_.get (core.js:12069)
at resolveDep (core.js:12559)
at resolvePromise (zone.js:821)
at resolvePromise (zone.js:785)
at eval (zone.js:870)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4724)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:594)
at <anonymous>

This is my functions: in the firebase.service.ts

  async login() {
    let u = await this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider())
    console.log(u);
    return this.getUserData(u.user.email);
  }

  public updateUser(UserName,phone)
  { 
    this.userRef.doc(this.getEmail()).set({
     UserName:UserName,
     phone:phone, 
    });

    this.router.navigate(["home"]);
  }

in the login.component.ts:

login(){

  this.firebaseService.updateUser(this.UserName,this.phone);

}

in the login.component.html:


        <input type="text" [(ngModel)]="UserName" onsubmit="myFunction()" name="UserName"  pattern="[A-Za-z]{}" placeholder="UserName">
        <div id="UserName" style="color:transparent; display:none">
          The user name is required.
        </div>

        <input type="number" [(ngModel)]="phone" name="phone" placeholder="Phone number">
        <div id="PelephoneNumber" style="color:transparent; display:none">
            The phone is required.
          </div>
        <button (click)=login() type="Submit" class="Submit" id="login-button">Login</button>

    </form>
    </div>

Any help?

1 answer

  • answered 2018-01-11 19:48 devworksgroup

    I think you need to share a bit more of your code here. We don't know that this.userRef is set to... without assuming you set that correctly, I'm going to write it as:

    this.fireStore.collection('users').doc(this.getUserEmail()).update({name:'John Doe'});

    1.) Notice I used update, and not set. Set will completely overwrite the document with whatever new data you pass it. I assume you want to use update as your function name is called "updateUser()"

    2.) I'm also assuming that this.getUserEmail() is returning a string. the .doc() method expects a string that matches the ID of the document you are trying to update.

    I can probably help more if you provided a bit more of your service code...