代码之家  ›  专栏  ›  技术社区  ›  Kevin Dias

无法访问内部变量。catch(函数(错误)离子3

  •  0
  • Kevin Dias  · 技术社区  · 8 年前

    尝试toast erros,但是我不能从construtor访问toast变量,这是未定义的,我不知道为什么。我在这里做错什么了?在其他的课程中,它工作得很好,巫婆很奇怪。误差 “错误类型错误:无法读取未定义的属性‘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 回复  |  直到 8 年前
        1
  •  1
  •   Pardeep Jain    8 年前

    使用箭头功能 => 而不是普通的函数回调。

    你的代码应该是-

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