Sending data from child components to parent components in Angular

I'm facing an issue with retrieving data from a child array named store within user data returned by an API. When trying to access this child array in my view, it keeps returning undefined.

Code

export class TokoPage implements OnInit {

  store= null;
  token: any;

  constructor(
    private authService: AuthService,
    private navCtrl: NavController,
    private storage: NativeStorage,
  ) {
    this.storage.getItem('token').then((token) => {
      this.token = token;
    }).catch(error => console.error(error));
  }

  ngOnInit() {
    this.authService.user().subscribe((user) => {
      console.log('auth user', user);
      console.log('1', user['sotre']); //undefined
    });
  }
}

View

{{store.name}}

returned data

https://i.sstatic.net/q4wjS.png

Any ideas on how to address this?

Update

JSON.stringify(user) results

{
    "success":[
        {
            "id":1,
            "name":"xxxxx",
            "username":"admin",
            "email":"xxxxx",
            "phone":"xxxxx",
            "avatar":null,"
            coins":0,
            "type":"admin",
            "email_verified_at":"2019-08-13 14:08:09",
            "created_at":"2019-08-13 14:08:09",
            "updated_at":"2019-08-13 14:08:09",
            "products":[{xxxxxxx}],
            "addresses":[],
            "wishlist":[],
            "orders":[],
            "store":{
                "id":1,
                "name":"xxxxx",
                "url":"xxxxx",
                "logo":null,
                "banner":null,
                "description":"xxxxx",
                "kota_id":xxxxx,
                "province_id":xxxxx,
                "province":"xxxxx",
                "kota":"xxxxx",
                "address":"xxxxx",
                "phone":"xxxxx",
                "user_id":1,
                "active":"yes",
                "verified":"yes",
                "best_seller":"yes",
                "created_at":"2019-08-13 14:08:09",
                "updated_at":"2019-08-15 09:17:40"
            },
            "rating":[]
        }
    ]
}

Answer №1

Initially, there appears to be a typographical error

console.log('1', user['store']);

Secondly, the way you are trying to access it is incorrect, success is actually an array

this.authService.user().subscribe((user: any) => {
  console.log('auth user', user.success[0]['store']);
});

let user = {
  "success": [{
    "id": 1,
    "name": "xxxxx",
    "username": "admin",
    "email": "xxxxx",
    "phone": "xxxxx",
    "avatar": "null",
    "coins": 0,
    "type": "admin",
    "email_verified_at": "2019-08-13 14:08:09",
    "created_at": "2019-08-13 14:08:09",
    "updated_at": "2019-08-13 14:08:09",
    "products": [],
    "addresses": [],
    "wishlist": [],
    "orders": [],
    "store": {
      "id": 1,
      "name": "xxxxx",
      "url": "xxxxx",
      "logo": null,
      "banner": null,
      "description": "xxxxx",
      "kota_id": "xxxxx",
      "province_id": "xxxxx",
      "province": "xxxxx",
      "kota": "xxxxx",
      "address": "xxxxx",
      "phone": "xxxxx",
      "user_id": 1,
      "active": "yes",
      "verified": "yes",
      "best_seller": "yes",
      "created_at": "2019-08-13 14:08:09",
      "updated_at": "2019-08-15 09:17:40"
    },
    "rating": []
  }]
};
console.log(user.success[0]['store']);

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

problems encountered when trying to deploy a backend api on the Render platform

Encountered this error: May 14, 04:27:30 PM - Error [email protected]: The "node" engine is not compatible with this module. Expected version ">=14.20.1". Received version "14.17.0". May 14, 04:27:30 PM - Incompatible module detected. Verified my ...

I am experiencing difficulties with bringing in node modules into my project

Trying to incorporate @mui/styles into my React project resulted in encountering some errors as shown below. npm ERR! Could not resolve dependency: npm ERR! peer react@"^17.0.0" from @mui/<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

Issue with Vue: After removing an item from a v-for list, there is a remnant

Can you help me solve this issue with my Vue component setup? I have a list of components, each consisting of a preview canvas, a name, and a delete button. These components are stored in Vuex. For example, the list could look like this: | [W] Item 1 ...

Setting configuration files in Node.js with npm configuration

I have developed a SAAS application on the Angular/NodeJS/Postgres+MongoDB stack that can establish connections with customer databases, cloud warehouses, S3 buckets, and more to load relevant information. Once I receive connection details from the Angular ...

Mobile Safari on iOS devices is known for its capability to overlay HTML5 <video> on top of everything

Although a similar question was asked 6 years ago without any answers, I am currently facing the same issue. Problem: The <video> tag in my Angular/Ionic application overlaps my image and two buttons in iOS Mobile Safari - no matter what I try! It ...

Ajax is working effectively as the first post is successful and the second post is able to retrieve data

UPDATE: I resolved the issue by relocating my form submitter script from the Header to the bottom of the jamesmsg.php page (the included one). By doing this, the functions are always re-loaded and attached to the "new" form each time the div is refreshed. ...

Tips for decreasing the width of a Grid component in React using MUI

Is there a way to adjust the width of the initial Grid element within Material UI, allowing the remaining 3 elements to evenly occupy the extra space? see visual example Would modifying the grid item's 'xl', 'lg', 'md', ...

Callback after completion of a for loop in Angular TypeScript

During the execution of my javascript async function, I encountered a situation where my printing code was running before the div creation. I needed to ensure that my print code would run only after the completion of the for loop, but I struggled to find a ...

Add the word "String" in front of the moment timestamp

In my project, I have used React along with Material UI to show the message Running check... when the clicked state is set to true. If it's not true, then I am displaying a timestamp by utilizing the react-moment library. <Typography gutterBottom ...

Receiving constant errors on Search Input in React: continuously encountering the error "Cannot read property 'value' of undefined."

Attempting to incorporate the < ChipInput /> element from https://github.com/TeamWertarbyte/material-ui-chip-input. Using material-UI react component, my current setup includes: A functional search input bar. When an artist's name is typed, it ...

Limit the covariance of properties in generic arguments

When I define a generic argument of object type with specific mandatory properties (for example, in this function it requires the object to have timestamp: string), TypeScript allows for using a more specialized attribute type as a generic argument - as sh ...

What is preventing me from setting a background image in Angular 13?

Trying a different approach based on advice from Stack Overflow, I attempted the following: <div [style.background-image]="'url(https://picsum.photos/200)'"></div> Unfortunately, this resulted in no effect and the image was ...

Sync State with Data from API Query

I have a scenario where I need to fetch data from two different APIs and update their states separately. Afterward, I am iterating through the NBA data and looping through the animeData object to update the allData state, which will be an array of object ...

Discovering the final element of ng-content while conducting tests using Protractor

I am searching for the always last element to click on. This is what I see when I inspect the page. https://i.sstatic.net/DMXQ8.png https://i.sstatic.net/ZkbER.png ...

Calculation Error in JavaScript/JQuery

I've been working on a JavaScript function to calculate the sum of values entered into textboxes, but it seems to be giving me inaccurate results in certain cases. Check out the FIDDLE here Enter values : 234.32 and 32.34 Result: 266.6599999999999 ...

The icon for the weather on openweathermap is currently not displaying

Take a look at what my webpage looks like: http://prntscr.com/dg6dmm and also check out my codepen link: http://codepen.io/johnthorlby/pen/dOmaEr I am trying to extract the weather icon from the api call and display that icon (e.g. "02n") on the page base ...

Guide on invoking personalized server-side functions (such as object parsing) utilizing Typescript and Angular tools

I've been grappling for weeks to make custom service calls function with Typescript / Angular / C#. It's been a challenge to find a workable solution online, and the more I search, the more bewildered I become. My current approach has largely be ...

Utilizing JavaScript to transition a grayscale thumbnail image to vibrant color

I am a beginner in the world of web development and I have no idea how to begin coding a JavaScript function that will smoothly transition a grayscale thumbnail image into a color thumbnail image when the user hovers their mouse over it, and then back to g ...

Google maps are failing to display on the page when loading via ajax

When I click a button, I use AJAX to load a page, but the map on the loaded page is not displaying. There are no errors showing up either. I have tried adding initmap() after the ajax load, but it doesn't work. Do I need to bind it somehow? What am I ...

Ways to showcase the contents of a React JavaScript document, or more specifically, a file designed for React interpretation

After trying multiple solutions, I found a conceptual guide on How to call and form a React.js function from HTML. However, the HTML generated doesn't seem to be calling from the JavaScript file as expected. It appears identical to what is mentioned i ...