Guide on how to retrieve the information stored in an object

I am experiencing an issue with my function that retrieves data from Firebase. I am able to read the objects, but I cannot access the properties within them.

Whenever I try to parse the content, an error occurs.

Here is the function in question:

 this.afAuth.authState.subscribe(data => {
            this.itemsList = this.afDatabase.list(`oders/${data.uid}`).valueChanges();
            
            console.log(this.itemsList)
            this.itemsList.forEach(data =>{
              console.log(data) // this works
              console.log(data.name) // this doesn't work (undefined in console)
            })
          })

This is the JSON OBJECT:

(14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {hour: 1567395936608, id: 0, name: "Salami", price: "8"}
1: {hour: 1567395936608, id: 9, name: "Suplémment Fromage", price: "8"}
2: {hour: 1567395936608, id: 4, name: "bolo", price: "8"}
3: {hour: 1567395936608, id: 4, name: "bolo", price: "8"}
4: {hour: 1567395985542, id: 4, name: "bolo", price: "8"}
5: {hour: 1567395985542, id: 0, name: "Salami", price: "8"}
6: {hour: 1567395985542, id: 9, name: "Suplémment Fromage", price: "8"}
7: {hour: 1567397849623, id: 6, name: "Dems", price: "15"}
8: {hour: 1567397849623, id: 0, name: "Salami", price: "8"}
9: {hour: 1567397849623, id: 9, name: "Suplémment Fromage", price: "8"}
10: {hour: 1567398309241, id: 0, name: "Salami", price: "8"}
11: {hour: 1567398309241, id: 9, name: "Suplémment Fromage", price: "8"}
12: {hour: 1567402336588, id: 0, name: "Salami", price: "8"}
13: {hour: 1567402336588, id: 9, name: "Suplémment Fromage", price: "8"}

Thank you!

Answer №1

To iterate over the elements in the `data` array, which is an array itself, you will need to use another loop.

One way to do this is:

this.itemsList.forEach(data =>{
    this.data.forEach(e=>{
        console.log(e.name) 
    })
})

Alternatively, you can try:

this.itemsList[0].forEach(data =>{
    console.log(data.name) 
})

Answer №2

Implementing the spread operator method.

[...this.itemsList].forEach(item) {
 console.log(item.hour);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for accessing data from a local JSON file in your React JS + Typescript application

I'm currently attempting to read a local JSON file within a ReactJS + Typescript office add-in app. To achieve this, I created a typings.d.ts file in the src directory with the following content. declare module "*.json" { const value: any; ex ...

Angular 7 application encountering error 500 with URL Rewrite Module on IIS7 server

I am facing an issue with my Angular 7 application hosted on IIS 7. The app should be accessible at https://example.com/fitcbooks/web. It was working fine initially, but suddenly stopped without any clear reason. Despite the fact that URL Rewrite on the se ...

Navigating to a child route from a parent component in Angular using the navigate method: A step-by-step guide

Here is the code snippet for my HTML routerLink: <a [routerLink]="['create']">Create</a> - it's working just fine. When I try to call the same on a button click, it doesn't work.. navigateTo(page) { this.router.na ...

Error occurs when attempting to test both boolean and number data within an ngIf statement

In the scenario where I am working with a template that includes a boolean called readOnly and an array known as arrayOfStuff: <span *ngIf="!readOnly && arrayOfStuff && arrayOfStuff.length">Hey</span> When running eitherng bui ...

I recently updated all my projects to Angular 14, but when I tried to install a package using `npm i`, it still

The challenge at hand I encountered an issue with my two Angular projects. The first project serves as a library utilized by the second project. I recently upgraded both projects to Angular 14 following this guide. However, after running an npm i on the ...

Surprising literal found at the second position

Encountered an error while trying to display a date as an array on an HTML page. The current implementation is causing an issue in the p-calendar tag within ngmodel, where date2[i] is being displayed based on the index i derived from p-datalist. I am retur ...

Guide to Re-rendering a component inside the +layout.svelte

Can you provide guidance on how to update a component in +layout.svelte whenever the userType changes? I would like to toggle between a login and logout state in my navbar, where the state is dependent on currentUserType. I have a store for currentUserTyp ...

What is the reason behind useEffect giving warnings for unnecessary fields that are not included in the dependencies array?

After reviewing the documentation for useEffect, I am puzzled by the warnings I receive for every variable and function used within useEffect, despite not having a dependency on them. Take a look at my useEffect below: const [updatedComm, setUpdatedComm] ...

What is causing the issue where search query parameters are not recognizing the initially selected option?

Hey, I'm having an issue with searchParams. The problem is that when I apply filters like "Breakfast, Lunch, Dinner", the first chosen option isn't showing up in the URL bar. For example, if I choose breakfast nothing happens, but if I choose lun ...

"Here's how you can mark an option as selected in Angular, either from the component or the HTML file

When it comes to my form, I have a select menu that sends data to an SQL database and then fetches it back when it is called for editing. The value being edited should be displayed in the select menu option as selected. Here's a peek at my code: < ...

Issues persist with debugger functionality in browser development tools following an upgrade from Angular 8 to version 15

After upgrading from Angular version 8 to version 15, I've encountered an issue where the debugger is not functioning in any browser's developer tools. Can anyone provide some insight on what could be causing this problem? Is it related to the so ...

Organize the attributes of components on the following line in Visual Studio Code

Is there a method to configure the VS code formatter for Angular 2+ templates to ensure that attributes are wrapped in a new line? I prefer this formatting: <app [input1]="input1$ | async" [input2]="input2$ | async" (inputChanged)="set ...

What is the most effective method for updating edited rows in a DataGrid MUI and transferring them to

Is there a way to update the values I just edited in firebase after using editRow (please correct me if I'm mistaken)? Within DataGrid //Enable row edit mode editMode="row" //Any updates made in the row are stored in this variable, overrid ...

Using Typescript to override an abstract method that has a void return type

abstract class Base{ abstract sayHello(): void; } class Child extends Base{ sayHello() { return 123; } } The Abstract method in this code snippet has a return type of void, but the implementation in the Child class returns a number. S ...

Angular 5 dynamically updates its model after the completion of events

The issue at hand Currently, I am working with angular 5 and have created a component that consists of 3 controls. <ss-multiselect-dropdown id="type" formControlName="type" (ngModelChange)="onChange('type')"></ss-multiselect-dropdown&g ...

Experiencing a specific build error on cloud build that does not occur during a docker build process

Challenges with gcloud Build Whenever I try to submit a build using gcloud, I encounter an error. Oddly enough, the build works perfectly fine on my local machine and even when creating a docker image locally. Despite my initial assumption that a file mig ...

Looking for a way to make certain pages in Vue3 accessible without authentication using KeyCloak-js

Currently, I am integrating keycloak-js adapter into a Vue 3 Application. The challenge I am facing is with the system's public pages, which prevent me from calling Keycloak immediately. Even though I have set up a button to trigger the login page, th ...

When working with the Sequelize-Typescript One To Many Association and Repository, a situation may arise where the query returns only one child entity even though there are multiple

Dealing with Sequelize-Typescript, I recently encountered the one-to-many association involving "Album" and "Photos" entities. Each "Album" can have multiple "Photos". Below are the entity codes for reference: Album.ts ` @Table({ timestamps: true, de ...

Sorting arrays in Typescript

Is there a way to alphabetically sort an array of objects in Typescript based on a specific field? The array I have currently looks like this - https://i.stack.imgur.com/fQ3PA.png I'm interested in sorting it alphabetically by the 'channel' ...

Creating an Account with Firebase

I've created a function called signup in my web project that uses JavaScript to add a user to my Firebase database. The function works fine, but I'm encountering an issue when I try to redirect to another page at the end of the function - the use ...