2

我试过阅读angularfire2文档,并且已经到了这里。我的身份验证服务调用angularfire.auth.login。我订阅了这个函数,我想将返回的userinfo保存到数据库中。如何验证用户并保存他们的详细信息

打字稿编译器返回错误:

Error: Typescript found the following errors: /Users/macbook/dev/app/kichenclub/tmp/broccoli_type_script_compiler-input_base_path-nMnfGgj9.tmp/0/src/app/auth.service.ts (12, 25): Property 'displayName' does not exist on type 'FirebaseAuthState'.

和同样适用于所有 '显示名'。

我在正确的方向,甚至?如何解决这个问题。

authentication.service.ts:

import { Injectable } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2'; 

@Injectable() 
export class AuthService { 
    private userModel= this.af.database.object('/users'); 
    constructor(private af: AngularFire) { 
    this.af.auth.subscribe((auth) => { 
     console.log(auth); 
     this.userModel.set({ 
     uid: auth.uid, 
     firstName: auth.displayName.split(0, auth.displayName.lastIndexOf(' ')), 
     lastName: auth.displayName.split(auth.displayName.lastIndexOf(' '), auth.displayName.length), 
     displayPic: auth.photoURL, 
     provider:auth.provider 
     }); 
    }); 
    } 

    public loginFacebook(): void { 
    this.af.auth.login({ 
     provider: AuthProviders.Facebook, 
     method: AuthMethods.Popup 
    }) 
    } 
} 

让我知道,如果需要任何进一步的信息。

最后,谢谢你停下来。 :)

回答

2

我看了一下GitHub上的angularfire2源代码,并检查了FirebaseAuthState接口,你可以找到它here。您可以看到TypeScript接口本身没有名为'displayName'的属性。但是它确实表明,有出现来保存数据的可空类型,你正在寻找

export interface FirebaseAuthState { 
    uid: string; 
    provider: AuthProviders; 
    auth: firebase.User; 
    expires?: number; 
    github?: CommonOAuthCredential; 
    google?: GoogleCredential; 
    twitter?: TwitterCredential; 
    facebook?: CommonOAuthCredential;//<---based on your code I think this is what you're looking for. 
    anonymous?: boolean; 
} 

我想你将要采取的auth.facebook财产一探究竟。为了更好地了解细节,我只是简单地在您的订阅方法中注释掉代码,并将对象写入控制台,并使用浏览器开发工具在浏览器中查看它。例如像这样的东西。

this.af.auth.subscribe((auth) => { 
    console.log(auth); 
    //this.userModel.set({ 
    //uid: auth.uid, 
    //firstName: auth.displayName.split(0, auth.displayName.lastIndexOf(' ')), 
    //lastName: auth.displayName.split(auth.displayName.lastIndexOf(' '), auth.displayName.length), 
    //displayPic: auth.photoURL, 
    //provider:auth.provider 
    //}); 
}); 

这将允许您仔细观察auth.facebook中的数据。

+0

谢谢@dan。其实,我经历了日志记录并通过了。实际上,我错过了'auth.auth'实际上保存了数据。我的错。我想我的咖啡因很高,错过了这一点。 :p – ankitjain11

相关问题