Firebase Firestore get()无效

Mar*_*ark 9 firebase typescript angularfire2 angular google-cloud-firestore

它不喜欢的部分是ngOnInit()中的get()方法.是的,"[ts]属性'get'在类型'AngularFirestoreDocument <{}>'上不存在."

我查看了这里:https://firebase.google.com/docs/firestore/query-data/get-data ,它显示了对单个文档使用get()方法,但它只是不喜欢我的方法?

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(['']);
}
Run Code Online (Sandbox Code Playgroud)

}

cot*_*riv 18

实际上,AngularFirestoreDocument<{}>没有get财产,AngularFirestoreDocument<{}>.ref改为使用:

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);
            });
Run Code Online (Sandbox Code Playgroud)

  • 确切地说,该文档指导所有作为"Javascript SDK",但您使用的是"angularfire2",它是为"angular"项目定制的. (3认同)