I have the following error in my ionic project "Error: Uncaught (in promise): TypeError: Can not read property '0' of undefined"

0

Hello my Ionic project has the following error (Error: Uncaught (in promise): TypeError: Can not read property '0' of undefined) Attached is an image of the problem. I would like some hint Followtheprojectlink.

link

         login.ts

        import { Component } from '@angular/core';
       //import { NgForm } from '@angular/forms';

       import { NavController, ToastController } from 'ionic-angular';

        import { UserData } from '../../providers/user-data/user-data';
         import { AuthService } from 
       "../../providers/authservice/authservice";
        import { UserOptions } from '../../components/user-options/user-
         options';
       import { Storage } from "@ionic/storage";
        import { UserPage } from '../user/user';


        @Component({
           selector: 'page-user',
         templateUrl: 'login.html'
          })
       export class LoginPage {
         login: UserOptions = { username: '', password: '' };
       userData= {"username":"", "password":""};
         responseData: any;
       public userInfo: any;
        public dataSet: any;

       submitted = false;

        constructor(
        public navCtrl: NavController, 
       public loginData: UserData, 
       public authService: AuthService,
        public storage: Storage,
       public toastCtrl: ToastController
        ) { }

     //onLogin(form: NgForm) {
      onLogin() {
       this.submitted = true;
       this.dataSet = [];
      //var userdatalocal;

      if(this.userData.username && this.userData.password){
      //API connection
       this.authService.postData(this.userData, "login")
      .then((result) => {
       this.responseData = result;

      if(this.responseData){
      this.dataSet = this.responseData;


     //localStorage.setItem('userData',JSON.stringify(this.responseData));
     //this.storage.set('userData',JSON.stringify(this.responseData));
     this.userInfo = this.dataSet.userId[0].userData;
     // console.log(this.dataSet.userId[0].userData.email);

      this.loginData.login(this.userData.username); //para setar flag 
       loggedInMenu

      this.navCtrl.push(UserPage,this.dataSet);
    } else {
      // If login or password is invalid then return to LoginPage
      console.log("Erro18r: login/password invalid or bad connection");
      this.navCtrl.setPages([
        {page: LoginPage}
      ]) ;
      let toast = this.toastCtrl.create({
        message: this.responseData.error.text ,//'Login or password 
       incorrect.',
        duration: 5000
      });
      toast.present();
       }
       }, (err) => {
        //Connection failure message
       console.log(err);
        this.navCtrl.setPages([
         {page: LoginPage}
        ]) ;
          })
         } 
        }

          }
    
asked by anonymous 18.01.2018 / 17:05

3 answers

0

Man, the error occurs in this this.userInfo = this.dataSet.userId[0].userData; statement, you have to check that the returned object is valid and you are accessing it correctly.

    
19.01.2018 / 18:55
0

The object is not being mounted ... probably here

"responseData = result;"

Change to

"responseData = result.json();"

I think that's what's wrong.

    
01.10.2018 / 21:35
0

I can not see the image of where I am now because of a proxy. It may not seem like it, but you're expecting the '0' property of some object that was not instantiated (maybe an array). What can happen is that this object can be instantiated late, that is, after the moment when the ionic is trying to get the value.

Another thing, is that the ionic cli is half capenga (at least on windows), so you make an alteration, wait for the refresh and simply return an old version, at that moment or some refresh later. This is easily solved by killing the process and giving ionic serve again.

If the problem is really getting value from an object not instantiated, you can use console.log to check the value / instance, debug .ts for chrome, debugar by vscode (if you are using it), or even try to put '?' just after the name of the object where it is being used. In other words, you need to find out what is the object that is without an instance.

    
18.01.2018 / 17:25