简体   繁体   English

如何从angular 9中的firebase实时数据库中检索单品详情并打印到html中?

[英]How to retrieve single product details from firebase realtime database in angular 9 and print it in html?

product.component.ts product.component.ts

import { AngularFireDatabase } from '@angular/fire/database';
import { ProductService } from './../product.service';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit} from '@angular/core';


@Component({
  selector: 'app-product',
  providers:[],
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {

  product;
  object9;
  constructor(public route: ActivatedRoute,public db: AngularFireDatabase) { 

      this.id= this.route.snapshot.paramMap.get('id');

    console.log(this.id);

    this.object9=this.db.object('products/'+this.id).valueChanges().subscribe(val =>{ 
          console.log(val);
        });

   }

 ngOnInit() {

  }

}

product.component.html

{{ object9.fullName|async|json}}

Firebase realtime Database: Firebase 实时数据库:

oshop-1df92

   products

     -M9vLP-mF2DAIMkkKVR_
        email:
        fullName:

     -M9vLfD2r3PrQbXQiYot
     -M9vSjV9lNVZ2QhIj63n

In console I am getting id printed and console.log(val) prints object having the id,but in html it is printing null.Why??在控制台中,我打印了 id,console.log(val) 打印了具有 id 的 object,但在 html 中打印的是 null。为什么? I am using latest angular 9 with latest version of firebase and angularfire.Oshop is name of project.我正在使用最新的 angular 9 和最新版本的 firebase 和 angularfire.Oshop 是项目的名称。 Please help me out.请帮帮我。

By assigning the subscription to the variable object9 and not the data that you get from the subscription.通过将订阅分配给变量object9而不是您从订阅中获得的数据。 Instead replace your code of assinging the value by the following lines:而是用以下几行替换您分配值的代码:

   this.db.object('products/'+this.id).valueChanges().subscribe(val =>{ 
      console.log(val);
     this.object9= val
   });
   }

And now simply print the value in html using:现在只需使用以下命令打印 html 中的值:

{{object9.fullname}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase实时数据库中检索数据? - How to retrieve data from firebase realtime database? 无法将数据从Firebase实时数据库检索到Web应用程序 - Unable to retrieve data from firebase realtime database into web app Firebase实时数据库:无法从Firebase数据库发送或检索数据 - Firebase Realtime Database: Can't send or retrieve data from Firebase Database 如何从Firebase实时Android到Web应用程序检索数据 - How to retrieve data from Firebase realtime android to web app 如何使用multer上传文件并将详细信息存储在mean rest应用程序的数据库中,以及如何从angular.js检索文件 - How to upload file using multer and store details in database in mean rest app and retrieve it from angular.js HTML表显示Firebase实时数据库数据 - HTML table to display Firebase Realtime Database data 使用 HTML 按钮写入 Firebase 实时数据库不起作用 - Writing to the Firebase Realtime Database with an HTML button is not working 如何从HTML表单中检索密码然后打印 - How To Retrieve password From HTML Form and Then Print It 从SQL数据库检索记录并以HTML打印(通过Python) - Retrieve record from SQL database and print in HTML (via Python) 如何使用 Angular 检索 firebase 数据库内容的所有 id? - How to retrieve all id of firebase database content using Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM