Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从Firebase数据库对象获取值_Angular_Typescript_Firebase_Firebase Realtime Database - Fatal编程技术网

Angular 从Firebase数据库对象获取值

Angular 从Firebase数据库对象获取值,angular,typescript,firebase,firebase-realtime-database,Angular,Typescript,Firebase,Firebase Realtime Database,我只是试图从firebase数据库中获取用户名值,并将其显示在控制台日志语句中,但在执行此操作时遇到了问题。这就是我目前所拥有的 现在它所做的只是获取对象,而不是实际的子值。如何获取子值以便可以显示它 getUsername.ts import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams} from 'ionic-angular'; import { AngularFireAu

我只是试图从firebase数据库中获取用户名值,并将其显示在控制台日志语句中,但在执行此操作时遇到了问题。这就是我目前所拥有的

现在它所做的只是获取对象,而不是实际的子值。如何获取子值以便可以显示它

getUsername.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable  } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-profile-setup',
  templateUrl: 'profile-setup.html',
})
export class ProfileSetupPage {
  profileData: FirebaseObjectObservable<Profile>

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase) {

  showUsername($event) {
    let data = this.profileData = this.afDatabase.object(`profile/`)
    console.log(data.username);
  }
 }
}
从'@angular/core'导入{Component};
从“离子角度”导入{IonicPage,NavController,NavParams};
从'angularfire2/auth'导入{AngularFireAuth};
从“angularfire2/database”导入{AngularFireDatabase,FirebaseObjectObservable,FirebaseListObservable};
@IonicPage()
@组成部分({
选择器:“页面配置文件设置”,
templateUrl:'profile setup.html',
})
导出类配置文件设置页面{
profileData:FirebaseObjectObservable
构造函数(专用afAuth:AngularFireAuth,专用afDatabase:AngularFireDatabase){
showUsername($event){
让data=this.profileData=this.afDatabase.object(`profile/`)
console.log(data.username);
}
}
}

您必须先订阅数据并将其放入对象中才能使用数据。

定义名为
用户(用户名、年龄)
的模型(接口)并导入
getUsername.ts
。您还需要传递唯一的
userid
作为参数

showUsername($event) {
    let data = this.afDatabase.object(`profile/`+<string>userid);
    data.valuechanges().subscribe( user => console.log(user.username));
  } 
showUsername($event){
让data=this.afDatabase.object(`profile/`+userid);
data.valuechanges().subscribe(user=>console.log(user.username));
} 

您需要以某种方式使用对象的ID来定位用户名。这是不可避免的。您可以将其添加到查询中,也可以在访问对象时使用它。好的,如果我添加profile/WhateverIDIs,如何获得实际的用户名值?执行查询并记录其结果。如何找到你要寻找的领域应该是显而易见的。我根本不与Angular合作,所以我不知道具体细节。好的,谢谢你的帮助!