在回调中找不到Angular 4 firebase //功能

时间:2017-05-23 11:55:07

标签: angular firebase

希望你能帮助我。我试图从firebase获得回调。这是我的代码

select A.col1, A.col2 from A where ...
UNION
select B.col1, B.col2 from B where ...
UNION
...

我尝试在回调中调用createAccount() { this.afAuth.auth.createUserWithEmailAndPassword(this.email, this.password) .then(function (message) { console.log(message); this.setUserProfile(); }) .catch(function (e) { console.log(e); }); } setUserProfile() { // some stuff }` 。问题是当我在回调中设置this.setUserProfile()时,他们不再能找到这个函数,console.log的运行没有任何问题。

1 个答案:

答案 0 :(得分:2)

使用arrow function来保留上下文。

this.afAuth.auth.createUserWithEmailAndPassword(this.email, this.password)

        .then((message) => {      //  <---- use error function here
            console.log(message);
            this.setUserProfile();
        })
        .catch(function (e) {     //  <---- if you use "this" in error callback, change it to arrow function too
            console.log(e);
        });