从AngularFire中观察到的Angular模板中的ID用户不起作用

发布于 2025-01-21 11:44:21 字数 468 浏览 0 评论 0原文

我具有通过ID获取用户的功能,但总是可以返回可观察的,但是我的问题是,我必须将ID传递给函数表格模板以获取我想要的数据,因为该功能不是我不知道的普通函数如何在角度做到这一点。

/user.service
returnuserbyid(id) {
    const noteDocRef = doc(this.fs, `users/${id}`);
    return docData(noteDocRef, { idField: 'id' }) as Observable<any>;

/user.page
 getuser(id) {
    this.userservice.returnuserbyid(id).subscribe((res) => {
      this.user = res;
    });

我希望能够将ID传递给模板内的Getuser函数并获取用户的详细信息。我目前正在使用Angular Fire 7

i have the function to get user by id but it always return observable, but my problem is i have to pass and id to the function form the template in order to get the data i want, since the function isnt an ordinary one i dont know how to do that in angular.

/user.service
returnuserbyid(id) {
    const noteDocRef = doc(this.fs, `users/${id}`);
    return docData(noteDocRef, { idField: 'id' }) as Observable<any>;

/user.page
 getuser(id) {
    this.userservice.returnuserbyid(id).subscribe((res) => {
      this.user = res;
    });

i want to be able to pass pass an id to the getuser function inside my template and get the details of the user. i an currently using angular fire 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2025-01-28 11:44:21

有两种方法可以通过firebase

通过ID检索用户,

  • 如果是firebase

    默认定义的ID,则使用了第一个方法。

     构造函数(私有ngfirestore:angularfirestore){}
    
    getuserbydefaultId(id){
     返回this.ngfirestore.collection('用户')。doc(id).valuechanges()
    }
     
  • 如果您自己给用户提供ID(此ID是用户的一部分),则
    字段)

     构造函数(私有ngfirestore:angularfirestore){}
    getuserbyId(uid){
     返回this.ngfirestore.collection('用户',ref =&gt; ref.where('id','==',uid))。valuechanges();
     }
    //最终用户服务
     

顶部两个部分用于用户服务,以下内容用于
在用户页面

//user.page
constructor(private userservice: UserService) {}
user:any;
getuser(id) {
    this.userservice.getUserById(id).subscribe((res) => {
      this.user = res;
});

//full example

<!-- user component html -->
<form (ngSubmit)="logForm(form)" #form="ngForm">
    <ion-input name="id" placeholder="Enter id"></ion-input>
    <button ion-button type="submit" block>Add Todo</button>
</form>>

<!-- user component ts-->
import { NgForm } from '@angular/forms';

constructor(private userservice: UserService) {}
user:any;
getuser(id) {
    this.userservice.getUserById(id).subscribe((res) => {
      this.user = res;
});

logForm(form:NgForm){

    const id_user=form.value['id'];
    this.getuser(id_user);
    //now let's display the recovered user
    console.log(this.user)
}

There are two ways to retrieve a user through the id with firebase

  • the first one if it is an id defined by default by firebase

    constructor(private ngFirestore: AngularFirestore) {}
    
    getUserByDefaultId(id) {
     return this.ngFirestore.collection('users').doc(id).valueChanges()
    }
    
  • if you give an id to the user yourself (this id is part of the user
    field)

    constructor(private ngFirestore: AngularFirestore) {}
    getUserById(uid){
     return this.ngFirestore.collection('users', ref => ref.where('id', '==', uid)).valueChanges();
     }
    //end user.service
    

the top two parts are for the user service and the following is used
in the user page

//user.page
constructor(private userservice: UserService) {}
user:any;
getuser(id) {
    this.userservice.getUserById(id).subscribe((res) => {
      this.user = res;
});

//full example

<!-- user component html -->
<form (ngSubmit)="logForm(form)" #form="ngForm">
    <ion-input name="id" placeholder="Enter id"></ion-input>
    <button ion-button type="submit" block>Add Todo</button>
</form>>

<!-- user component ts-->
import { NgForm } from '@angular/forms';

constructor(private userservice: UserService) {}
user:any;
getuser(id) {
    this.userservice.getUserById(id).subscribe((res) => {
      this.user = res;
});

logForm(form:NgForm){

    const id_user=form.value['id'];
    this.getuser(id_user);
    //now let's display the recovered user
    console.log(this.user)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文