How do I access this information in Angular? [closed]

How do I access this information in Angular? [closed]



I can't access the information in this object



screenshot



The ts file


import Component from '@angular/core';
import AngularFireDatabase, AngularFireObject from
'angularfire2/database'
import Observable from 'rxjs';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)
export class AppComponent
title:AngularFireObject<any>;
message: Observable<any>;

constructor(private db: AngularFireDatabase)
this.title = db.object('message');
this.message = this.title.valueChanges();





and the html file:


<p> message </p>



But I got this in my HTML Page:



"-LKbddlCLBERGd6y4MD2":
"email": "rakiubl97@gmail.com",
"message": "sa",
"timeStand": "2018/8/23 16:38:12",
"username": "Rakibul"
,
... this repeats a similar object many times



I want to access them one by one. Whatever I like to access.



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Have you code something?
– Michael Montero
Aug 25 at 15:53






I added the photo. Please check now
– Moumita akter
Aug 25 at 15:56





You've added a photo of the Firebase data, not what you have try already
– Michael Montero
Aug 25 at 15:57






I added the code also. Please check
– Moumita akter
Aug 25 at 16:06





Hi @Moumitaakter! Welcome to StackOverflow! Please clarify what you're asking for in your question. Are you asking for how to access all of the data in your database in the messages property, or are you asking for how to access only an individual property in your messages property?
– Edric
Aug 27 at 7:19


messages


messages




2 Answers
2



You can access the properties of the object using the for-in loop. You can push the contents of the object into an array and then run *ngFor over that array to access any property.


for-in


*ngFor


for (var key in message)
console.log('Email: ', message[key].email);

// push message[key] into an array
messages.push(message[key]);



HTML:-


<p *ngFor="let m of messages"> m.email </p>



Running code snippet:-




var message =
'lkdp':
email: 'email 1'
,
'lkdq':
email: 'email 2'
,


for (var key in message)
console.log('Email: ', message[key].email);





Thaks Ankit but Its only access the local object. I want to access the database object.
– Moumita akter
Aug 25 at 16:26





@Moumitaakter - Then you need to make an API call. I thought you have this data present at the front end side already
– Ankit Sharma
Aug 25 at 16:28





Yes I have the data
– Moumita akter
Aug 25 at 16:34





@Moumitaakter - Then whats the problem. The above solution should work just fine for you, is it not? Ofcourse you will have to convert message to this.message in case that is a class property. Also, you can always convert the var keyword to const.
– Ankit Sharma
Aug 25 at 16:34



message


this.message


var


const



NOTE: Please clarify what you're asking for in your question. Are you asking on how to access all of the data at once? Or, are you asking on how to access only one property? The solution below is answering the former question (AKA How do I access all of the data in the messages property at once?).


messages



That's because you're using the AngularFireDatabase#object method which only returns an object.


AngularFireDatabase#object



What you're looking for is the AngularFireDatabase#list method:


AngularFireDatabase#list


import Component from '@angular/core';
import AngularFireDatabase, AngularFireList from 'angularfire2/database';
import Observable from 'rxjs';

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)
export class AppComponent
title: AngularFireObject<any>;
message: Observable<any>;

constructor(private db: AngularFireDatabase)
this.title = db.list('message');
this.message = this.title.valueChanges();






Sorry, Edric. I'm new in StackOverflow. And it's my very first question. I'm asking on how I access a single piece of data in the messages property? I already figure it our how to access the whole message property data at once.
– Moumita akter
Aug 27 at 15:43






@Moumitaakter Sorry to ask, but are you only accessing one specific item, or do you want to access a random item from the property?
– Edric
Aug 28 at 0:17

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)