gpt4 book ai didi

angular - Firebase Firestore get() 不工作

转载 作者:太空狗 更新时间:2023-10-29 17:12:32 25 4
gpt4 key购买 nike

它不喜欢的部分是 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(['']);
}

最佳答案

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

关于angular - Firebase Firestore get() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47821073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com