无法访问.catch中的变量(函数(错误)Ionic 3

时间:2018-06-19 10:34:33

标签: angular typescript ionic3

尝试烘烤错误,但我无法从construtor访问toast变量说是未定义的,我不明白为什么。我在这做错了什么?在其他课程中它工作得很好,女巫很奇怪。错误 " ERROR TypeError:无法读取属性' toastCtrl'未定义"。任何帮助都会很棒

//Imports

import {
    Component,
    ChangeDetectorRef
} from '@angular/core';
import {
    NavController,
    NavParams,
    Platform,
    ToastController
} from 'ionic-angular';


import {
    Keyboard
} from '@ionic-native/keyboard';

import {
    Storage
} from '@ionic/storage';
import {
    fireUser
} from './../../objects/firebaseStructures';

import {
    MySegService
} from './../../services/myseg-api.service';



//Component

@Component({
    selector: 'page-login',
    templateUrl: 'login.html',
})

export class LoginPage {

    // Login details
    email: string;
    emailTest: string;
    loggedUser: fireUser;
    password: string;
    passwordTest: string;
    responseMessage: string;
    success: boolean = false;
    uid: string;
    user: string;
    registerUserPage = RegisterUserPage;
    forgetpass = ForgetPasswordPage;

    constructor(public navCtrl: NavController, public navParams: NavParams, public mysegService: MySegService, private platform: Platform,
        private keyboard: Keyboard, private storage: Storage, private toastCtrl: ToastController) {

        this.loggedUser = new fireUser(null, null, null, null, null, null, null, null, null, null, null, null, null, null); //user initialization
    }

    signInUser() {
        var user2: string;
        this.mysegService.loginUser(this.email, this.password).then(res => {
                console.log(res);
            })
            .catch(function (error) {
                // Handle Errors here.
                //unsuccessful log in
                var errorCode = error.code;
                var errorMessage = error.message;
                if (errorCode === 'auth/wrong-password') {
                    let toast = this.toastCtrl.create({
                        message: 'wrong password',
                        duration: 3000,
                        position: 'top'
                    });

                    toast.onDidDismiss(() => {
                        console.log('Dismissed toast');
                    });

                    toast.present();
                } else {
                    // alert(errorMessage);
                }
                console.log(error);
            });

    }

}

1 个答案:

答案 0 :(得分:1)

使用箭头函数=>代替普通函数回调。

您的代码应为 -

signInUser() {
        var user2: string;
        this.mysegService.loginUser(this.email, this.password).then(res => {
                console.log(res);
            })
            .catch((error) => {
                .......
            });

    }