【问题标题】:Undefined is not an object (evaluating 'type.parameters')未定义不是对象(评估'type.parameters')
【发布时间】:2017-05-03 18:21:59
【问题描述】:

我正在关注本教程:https://javebratt.com/angularfire2-email-auth/,但出现了一个奇怪的错误。

我在登录页面实现,这两个文件我已经实现了

login.ts

import {
  NavController,
  LoadingController,
  AlertController
} from 'ionic-angular';
import { Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { AuthData } from '../../providers/auth-data';

import { Page1 } from '../../pages/page1/page1';
import { SignupPage } from '../../pages/signup/signup';
import { ResetPasswordPage } from '../../pages/reset-password/reset-password';

import { EmailValidator } from '../../validators/email';


/*
  Generated class for the Login page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {
  public loginForm: any;
  emailChanged: boolean = false;
  passwordChanged: boolean = false;
  submitAttempt: boolean = false;
  public loading: any;

  constructor(public nav: NavController, public authData: AuthData,
public formBuilder: FormBuilder, public alertCtrl: AlertController,
public loadingCtrl: LoadingController) {
    this.loginForm = formBuilder.group({
      email: ['', Validators.compose([Validators.required, 
        EmailValidator.isValid])],
      password: ['', Validators.compose([Validators.minLength(6), 
      Validators.required])]
    });
  }


  goToResetPassword(){
    this.nav.push(ResetPasswordPage);
  }

  createAccount(){
    this.nav.push(SignupPage);
  }

  ionViewDidLoad() {
    console.log('Hello LoginPage Page');
  }

  elementChanged(input){
    let field = input.inputControl.name;
    this[field + "Changed"] = true;
  }

  loginUser(){
      this.submitAttempt = true;

      if (!this.loginForm.valid){
        console.log(this.loginForm.value);
      } else {
        this.authData.loginUser(this.loginForm.value.email, 
          this.loginForm.value.password).then( authData => {
            this.nav.setRoot(Page1);
      }, error => {
        this.loading.dismiss().then( () => {
          let alert = this.alertCtrl.create({
            message: error.message,
            buttons: [
            {
              text: "Ok",
              role: 'cancel'
            }
            ]
          });
        alert.present();
        });
      });

      this.loading = this.loadingCtrl.create({
        dismissOnPageChange: true,
      });
      this.loading.present();
      }
  }

}

和 auth-data.ts

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

import { AngularFire } from 'angularfire2';


@Injectable()
export class AuthData {
  fireAuth: any;
  constructor(public af:AngularFire) {
    console.log('Hello AuthData Provider');
    af.auth.subscribe(user => {
        if(user){
            this.fireAuth = user.auth;
            console.log(user);
        }
    })
  }
  loginUser(newEmail: string, newPassword: string): any {
    return this.af.auth.login({ email: newEmail, password: newPassword });
  }

  resetPassword(email: string): any {
    return firebase.auth().sendPasswordResetEmail(email);
  }

  logoutUser(): any {
    return this.af.auth.logout();
  }

  signupUser(newEmail: string, newPassword: string): any {
    return this.af.auth.createUser({ email: newEmail, password: newPassword });
  }
}

但是当我执行 'ionic serve' 时,我收到以下关于 login.ts 的奇怪错误

“AuthData”类型上不存在属性“loginUser”

这个 loginUser 函数在我的 auth-data.ts 中,所以我不明白它是什么意思。

我的猜测是“this.authData”从未启动,但即使我在构造函数中添加this.authData = authData,它也不会改变任何东西。 (这也说明我不明白它是如何启动的)

有人知道我为什么会收到此 loginUser 错误吗?

非常感谢

编辑:

感谢您在下面的一条建议,错误似乎已经消失,并且出现了一个新错误(即使现在说实话,即使我恢复更改,我仍然会收到新错误,我很困惑)

现在我在 Safari 中遇到的错误是 TypeError: undefined is not an object (evalating 'type.parameters') 而在 Firefox 中:TypeError: type is undefined[En savoir plus]

Firefox 控制台给了我

ReflectionCapabilitieshttp://localhost:8100/build/main.js:45824:1 反射器http://localhost:8100/build/main.js:45964:16 编译元数据解析器http://localhost:8100/build/main.js:25506:38 编译元数据解析器http://localhost:8100/build/main.js:25471:21 CompileMetadataResolverhttp://localhost:8100/build/main.js:25357:51 地图自托管 编译元数据解析器http://localhost:8100/build/main.js:25356:65 RuntimeCompiler http://localhost:8100/build/main.js:40363:24 RuntimeCompilercompileModuleAndComponents http://localhost:8100/build/main.js:40301:32 RuntimeCompiler http://localhost:8100/build/main.js:40292:16 PlatformRefbootstrapModuleWithZone http://localhost:8100/build/main.js:27765:16 PlatformRefhttp://localhost:8100/build/main.js:27747:16 http://localhost:8100/build/main.js:96568:1 webpack_require http://localhost:8100/build/main.js:20:12 http://localhost:8100/build/main.js:66:18

我很困惑,一定是错误地启动了一些东西,但错误堆栈并没有真正帮助......

【问题讨论】:

  • 您能否使用文件夹结构扩展您的示例,以便我们验证您正在导入正确的 auth-data.ts 文件?

标签: angular typescript firebase ionic2 angularfire


【解决方案1】:
this.authData.loginUser(this.loginForm.value.email, 
      this.loginForm.value.password)

您在这里错误地访问了表单参数。 loginUser 函数需要 2 个字符串参数。

试试:

this.loginForm.controls["email"].value
this.loginForm.controls["password"].value

编辑: 对于第二个错误,我猜这是问题所在:

 resetPassword(email: string): any {
    return firebase.auth().sendPasswordResetEmail(email);
  }

我在文件中的任何位置都没有看到 firebase 对象。

【讨论】:

  • 我认为这有帮助,谢谢,但现在我有另一个错误,让我更新我的初始帖子
  • 不是通过 import { AngularFire } from 'angularfire2' 导入的 firebase; ?好像是这样,sublime 不会给我一个错误,我得到的错误是在 html 端对吗?
  • 您已将其注入为 af..并在其他函数中使用了 this.af.auth
【解决方案2】:

我的猜测是'this.authData'从未启动,但即使我在构造函数中添加this.authData = authData,它也不会改变任何东西。 (这也说明我不明白它是如何启动的)

您不需要这样做,因为 Angular 注入器会负责提供程序的初始化。你可以阅读更多关于here

关于您的问题,您是否按照教程中的说明在应用程序根模块中导入并注册了您的提供程序?

【讨论】:

  • 我不认为这是这里的问题,否则会出现“没有提供者......”错误
猜你喜欢
  • 2016-12-04
  • 2019-09-26
  • 2019-12-12
  • 2019-08-16
  • 2017-01-16
  • 2020-08-06
  • 2020-01-09
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多