Firebase Firestore get() 不起作用 [英] Firebase Firestore get() not working

查看:30
本文介绍了Firebase Firestore get() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在这里查看:https://firebase.google.com/文档/firestore/查询数据/获取数据它显示对单个文档使用 get() 方法,但它只是不喜欢我的方法?

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(['']);
}

}

推荐答案

实际上,AngularFirestoreDocument<{}> 没有 get 属性,改用 AngularFirestoreDocument<{}>.ref:

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);
            });

这篇关于Firebase Firestore get() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆