Ionic firebase发送电子邮件验证

时间:2018-01-17 05:27:00

标签: angular typescript firebase ionic-framework firebase-authentication

我正在尝试在用户注册时向用户发送电子邮件验证。用户被添加到firebase但不发送电子邮件。我将错误记录到控制台并显示

  

无法读取未定义

的属性'sendEmailVerification'
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from "../../models/user";
import { AngularFireAuth } from "angularfire2/auth";
import * as firebase from 'firebase';


@IonicPage()
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage {

  user = {} as User

  constructor(private afAuth: AngularFireAuth,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  async register(user: User) {
     try {
       const result = await this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
       .then(res => {
         var auth = firebase.auth();
         this.auth.sendEmailVerification(user.email)
         this.navCtrl.setRoot('LoginPage');
       }
     }
     catch (e) {
       console.log(e);
     }
   }

}

2 个答案:

答案 0 :(得分:1)

首先你不能用它来引用变量,因为它是在同一个函数中本地声明的。

尝试删除此关键字,或者您可以选择按以下方式进行电子邮件验证:

注册后

在你的函数内部

 let user = firebase.auth().currentUser;
 user.sendEmailVerification();
 this.navCtrl.setRoot('LoginPage');

答案 1 :(得分:0)

看起来您使用的是this.auth.sendEmailVerification,但实际上您从未真正分配this.auth

尝试将this.auth.sendEmailVerification更改为auth.sendEmailVerification,因为您可以在this.afAuth.auth.sendEmailVerification之前或之前使用该变量。