Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore get() not working

The part it doesn't like is the get() method in ngOnInit(). Is says, "[ts] Property 'get' does not exist on type 'AngularFirestoreDocument<{}>'."

I looked here: https://firebase.google.com/docs/firestore/query-data/get-data and it shows to use get() method for a single doc, but it just doesn't like that method for me?

import { Component, OnInit, Pipe } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';

import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { User } from './user';

@Component({
   selector: 'user-form',
   templateUrl: 'user-form.component.html'
})
export class UserFormComponent implements OnInit {
form: FormGroup;
title: string;
user = new User();
id;
userDoc: AngularFirestoreDocument<User>;
singleUser: Observable<User>;      

constructor(fb: FormBuilder, private afs: AngularFirestore, private _router: Router, private _route: ActivatedRoute) {

    //
    this.form = fb.group({
        //username: ['', Validators.required],
        email: ['', Validators.required],
        title: ['', Validators.required]
    })



}

ngOnInit() {
    this.title = "Update User";

    this._route.params.subscribe(params => {
        this.id = params["id"];
    });

    if(!this.id) {
        console.log("New User");
    } 
    else {


        this.afs.collection("users").doc(this.id)
        .get().then(function(doc) {
            if (doc.exists) {
                console.log("Document data:", doc.data());
            } else {
                console.log("No such document!");
            }
        }).catch(function(error) {
            console.log("Error getting document:", error);
        });


    }

}

//
submit() {
    console.log(this.user.title + " - " + this.user.email);

    if (this.id) {   
        this.afs.doc('users/' + this.id).update({
            title: this.user.title, 
            email: this.user.email  
        });   ;                                                       
    }
    else{            
        this.afs.collection('users').add({
            name: this.user.title,  
            email: this.user.email  
        });                         
    }

    this._router.navigate(['']);
}

}

like image 218
Mark Avatar asked Dec 14 '17 19:12

Mark


People also ask

What does Firebase firestore () do?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline.

How do I connect firestore to Python?

To connect to Firestore, Firebase first performs authentication. To get the credentials for authentication, click on project settings, and click on “service accounts“. In the “Service accounts” tab, you can find a code snippet for connecting to Google Firebase. Select python as the language and copy the code snippet.


1 Answers

Actually, AngularFirestoreDocument<{}> doesn't have get property, use AngularFirestoreDocument<{}>.ref instead:

this.afs.collection("users")
            .doc(this.id)
            .ref
            .get().then(function(doc) {
                if (doc.exists) {
                    console.log("Document data:", doc.data());
                } else {
                    console.log("No such document!");
                }
            }).catch(function(error) {
                console.log("Error getting document:", error);
            });
like image 59
coturiv Avatar answered Oct 06 '22 21:10

coturiv