Angular 2 Firebase how to get array in object












1















I am getting this array from my database :



enter image description here



By this function :



getMembers(){
this.db.list(`projects/${this.auth.userId}/${this.uploadService.pro.name}/teams/`).snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe((data:any) =>
console.log(data)
this.members = data;
)
}


How to display members in ngFor ?



My try (first ngFor is teams - second one is for members) :



<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of item.members">{{member}}</p> //does not work
</div>


Error for let member of item.member :




Cannot find a differ supporting object '[object Object]' of type
'object'. NgFor only supports binding to Iterables such as Arrays.




Teams are only - name:string - members are in teams.



export Interface Teams {
name: string;
}

export Interface Members {
name: string;
}


At first I upload team - for example "Programmers". Then I add members to that Team. And it looks like :



8468as6d84a6s8d4a6s8d4 {
name: Programmers;
members: {
FirstMember,
SeconndMemeber...
}
}









share|improve this question

























  • can you post JSON data here instead of Image? also what data does teams contains?

    – Pardeep Jain
    Nov 25 '18 at 17:48













  • what is teams here? your variable shows it should be just members

    – Sajeetharan
    Nov 25 '18 at 17:50











  • Edited my question

    – Angulandy2
    Nov 25 '18 at 17:54
















1















I am getting this array from my database :



enter image description here



By this function :



getMembers(){
this.db.list(`projects/${this.auth.userId}/${this.uploadService.pro.name}/teams/`).snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe((data:any) =>
console.log(data)
this.members = data;
)
}


How to display members in ngFor ?



My try (first ngFor is teams - second one is for members) :



<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of item.members">{{member}}</p> //does not work
</div>


Error for let member of item.member :




Cannot find a differ supporting object '[object Object]' of type
'object'. NgFor only supports binding to Iterables such as Arrays.




Teams are only - name:string - members are in teams.



export Interface Teams {
name: string;
}

export Interface Members {
name: string;
}


At first I upload team - for example "Programmers". Then I add members to that Team. And it looks like :



8468as6d84a6s8d4a6s8d4 {
name: Programmers;
members: {
FirstMember,
SeconndMemeber...
}
}









share|improve this question

























  • can you post JSON data here instead of Image? also what data does teams contains?

    – Pardeep Jain
    Nov 25 '18 at 17:48













  • what is teams here? your variable shows it should be just members

    – Sajeetharan
    Nov 25 '18 at 17:50











  • Edited my question

    – Angulandy2
    Nov 25 '18 at 17:54














1












1








1








I am getting this array from my database :



enter image description here



By this function :



getMembers(){
this.db.list(`projects/${this.auth.userId}/${this.uploadService.pro.name}/teams/`).snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe((data:any) =>
console.log(data)
this.members = data;
)
}


How to display members in ngFor ?



My try (first ngFor is teams - second one is for members) :



<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of item.members">{{member}}</p> //does not work
</div>


Error for let member of item.member :




Cannot find a differ supporting object '[object Object]' of type
'object'. NgFor only supports binding to Iterables such as Arrays.




Teams are only - name:string - members are in teams.



export Interface Teams {
name: string;
}

export Interface Members {
name: string;
}


At first I upload team - for example "Programmers". Then I add members to that Team. And it looks like :



8468as6d84a6s8d4a6s8d4 {
name: Programmers;
members: {
FirstMember,
SeconndMemeber...
}
}









share|improve this question
















I am getting this array from my database :



enter image description here



By this function :



getMembers(){
this.db.list(`projects/${this.auth.userId}/${this.uploadService.pro.name}/teams/`).snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe((data:any) =>
console.log(data)
this.members = data;
)
}


How to display members in ngFor ?



My try (first ngFor is teams - second one is for members) :



<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of item.members">{{member}}</p> //does not work
</div>


Error for let member of item.member :




Cannot find a differ supporting object '[object Object]' of type
'object'. NgFor only supports binding to Iterables such as Arrays.




Teams are only - name:string - members are in teams.



export Interface Teams {
name: string;
}

export Interface Members {
name: string;
}


At first I upload team - for example "Programmers". Then I add members to that Team. And it looks like :



8468as6d84a6s8d4a6s8d4 {
name: Programmers;
members: {
FirstMember,
SeconndMemeber...
}
}






angular typescript firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 17:54







Angulandy2

















asked Nov 25 '18 at 17:39









Angulandy2Angulandy2

241213




241213













  • can you post JSON data here instead of Image? also what data does teams contains?

    – Pardeep Jain
    Nov 25 '18 at 17:48













  • what is teams here? your variable shows it should be just members

    – Sajeetharan
    Nov 25 '18 at 17:50











  • Edited my question

    – Angulandy2
    Nov 25 '18 at 17:54



















  • can you post JSON data here instead of Image? also what data does teams contains?

    – Pardeep Jain
    Nov 25 '18 at 17:48













  • what is teams here? your variable shows it should be just members

    – Sajeetharan
    Nov 25 '18 at 17:50











  • Edited my question

    – Angulandy2
    Nov 25 '18 at 17:54

















can you post JSON data here instead of Image? also what data does teams contains?

– Pardeep Jain
Nov 25 '18 at 17:48







can you post JSON data here instead of Image? also what data does teams contains?

– Pardeep Jain
Nov 25 '18 at 17:48















what is teams here? your variable shows it should be just members

– Sajeetharan
Nov 25 '18 at 17:50





what is teams here? your variable shows it should be just members

– Sajeetharan
Nov 25 '18 at 17:50













Edited my question

– Angulandy2
Nov 25 '18 at 17:54





Edited my question

– Angulandy2
Nov 25 '18 at 17:54












1 Answer
1






active

oldest

votes


















3














you are getting error, because your members is object and object isn't iterable, and *ngFor supports only iterables for example arrays, maps and so on..., so you can:



use Object.keys(), which will return keys of objects in new array,



NOTE that Object isn't accessible in the template, so you should create a pipe or define a method in the component.ts which will do the job,
for example



objectKeys(obj) {
return Object.keys(obj);
}

<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let key of objectKeys(item.members)">{{item.members[key].name}}</p>
</div>


or another option you can use Object.values(), which will return values of objects in new array



objectValues(obj) {
return Object.values(obj);
}
<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of objectValues(item.members)">{{member.name}}</p>
</div>





share|improve this answer


























  • Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

    – Angulandy2
    Nov 25 '18 at 19:09











  • @Angulandy2 I have updated my answer, please check again

    – Artyom Amiryan
    Nov 25 '18 at 19:14











  • It cant work because objectValues got "item" which is not defined.

    – Angulandy2
    Nov 25 '18 at 19:16











  • error : item is not defined in function objectValues(obj) or objectKeys(obj)

    – Angulandy2
    Nov 25 '18 at 19:17













  • As you can see i did that

    – Angulandy2
    Nov 25 '18 at 19:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470142%2fangular-2-firebase-how-to-get-array-in-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














you are getting error, because your members is object and object isn't iterable, and *ngFor supports only iterables for example arrays, maps and so on..., so you can:



use Object.keys(), which will return keys of objects in new array,



NOTE that Object isn't accessible in the template, so you should create a pipe or define a method in the component.ts which will do the job,
for example



objectKeys(obj) {
return Object.keys(obj);
}

<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let key of objectKeys(item.members)">{{item.members[key].name}}</p>
</div>


or another option you can use Object.values(), which will return values of objects in new array



objectValues(obj) {
return Object.values(obj);
}
<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of objectValues(item.members)">{{member.name}}</p>
</div>





share|improve this answer


























  • Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

    – Angulandy2
    Nov 25 '18 at 19:09











  • @Angulandy2 I have updated my answer, please check again

    – Artyom Amiryan
    Nov 25 '18 at 19:14











  • It cant work because objectValues got "item" which is not defined.

    – Angulandy2
    Nov 25 '18 at 19:16











  • error : item is not defined in function objectValues(obj) or objectKeys(obj)

    – Angulandy2
    Nov 25 '18 at 19:17













  • As you can see i did that

    – Angulandy2
    Nov 25 '18 at 19:19
















3














you are getting error, because your members is object and object isn't iterable, and *ngFor supports only iterables for example arrays, maps and so on..., so you can:



use Object.keys(), which will return keys of objects in new array,



NOTE that Object isn't accessible in the template, so you should create a pipe or define a method in the component.ts which will do the job,
for example



objectKeys(obj) {
return Object.keys(obj);
}

<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let key of objectKeys(item.members)">{{item.members[key].name}}</p>
</div>


or another option you can use Object.values(), which will return values of objects in new array



objectValues(obj) {
return Object.values(obj);
}
<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of objectValues(item.members)">{{member.name}}</p>
</div>





share|improve this answer


























  • Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

    – Angulandy2
    Nov 25 '18 at 19:09











  • @Angulandy2 I have updated my answer, please check again

    – Artyom Amiryan
    Nov 25 '18 at 19:14











  • It cant work because objectValues got "item" which is not defined.

    – Angulandy2
    Nov 25 '18 at 19:16











  • error : item is not defined in function objectValues(obj) or objectKeys(obj)

    – Angulandy2
    Nov 25 '18 at 19:17













  • As you can see i did that

    – Angulandy2
    Nov 25 '18 at 19:19














3












3








3







you are getting error, because your members is object and object isn't iterable, and *ngFor supports only iterables for example arrays, maps and so on..., so you can:



use Object.keys(), which will return keys of objects in new array,



NOTE that Object isn't accessible in the template, so you should create a pipe or define a method in the component.ts which will do the job,
for example



objectKeys(obj) {
return Object.keys(obj);
}

<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let key of objectKeys(item.members)">{{item.members[key].name}}</p>
</div>


or another option you can use Object.values(), which will return values of objects in new array



objectValues(obj) {
return Object.values(obj);
}
<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of objectValues(item.members)">{{member.name}}</p>
</div>





share|improve this answer















you are getting error, because your members is object and object isn't iterable, and *ngFor supports only iterables for example arrays, maps and so on..., so you can:



use Object.keys(), which will return keys of objects in new array,



NOTE that Object isn't accessible in the template, so you should create a pipe or define a method in the component.ts which will do the job,
for example



objectKeys(obj) {
return Object.keys(obj);
}

<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let key of objectKeys(item.members)">{{item.members[key].name}}</p>
</div>


or another option you can use Object.values(), which will return values of objects in new array



objectValues(obj) {
return Object.values(obj);
}
<div *ngFor="let item of teams">
<p>{{item.name}}</p>
<p *ngFor="let member of objectValues(item.members)">{{member.name}}</p>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 19:25

























answered Nov 25 '18 at 19:04









Artyom AmiryanArtyom Amiryan

1,9621214




1,9621214













  • Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

    – Angulandy2
    Nov 25 '18 at 19:09











  • @Angulandy2 I have updated my answer, please check again

    – Artyom Amiryan
    Nov 25 '18 at 19:14











  • It cant work because objectValues got "item" which is not defined.

    – Angulandy2
    Nov 25 '18 at 19:16











  • error : item is not defined in function objectValues(obj) or objectKeys(obj)

    – Angulandy2
    Nov 25 '18 at 19:17













  • As you can see i did that

    – Angulandy2
    Nov 25 '18 at 19:19



















  • Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

    – Angulandy2
    Nov 25 '18 at 19:09











  • @Angulandy2 I have updated my answer, please check again

    – Artyom Amiryan
    Nov 25 '18 at 19:14











  • It cant work because objectValues got "item" which is not defined.

    – Angulandy2
    Nov 25 '18 at 19:16











  • error : item is not defined in function objectValues(obj) or objectKeys(obj)

    – Angulandy2
    Nov 25 '18 at 19:17













  • As you can see i did that

    – Angulandy2
    Nov 25 '18 at 19:19

















Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

– Angulandy2
Nov 25 '18 at 19:09





Cannot read property 'values' of undefined or Cannot read property 'key' of undefined

– Angulandy2
Nov 25 '18 at 19:09













@Angulandy2 I have updated my answer, please check again

– Artyom Amiryan
Nov 25 '18 at 19:14





@Angulandy2 I have updated my answer, please check again

– Artyom Amiryan
Nov 25 '18 at 19:14













It cant work because objectValues got "item" which is not defined.

– Angulandy2
Nov 25 '18 at 19:16





It cant work because objectValues got "item" which is not defined.

– Angulandy2
Nov 25 '18 at 19:16













error : item is not defined in function objectValues(obj) or objectKeys(obj)

– Angulandy2
Nov 25 '18 at 19:17







error : item is not defined in function objectValues(obj) or objectKeys(obj)

– Angulandy2
Nov 25 '18 at 19:17















As you can see i did that

– Angulandy2
Nov 25 '18 at 19:19





As you can see i did that

– Angulandy2
Nov 25 '18 at 19:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470142%2fangular-2-firebase-how-to-get-array-in-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'