Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果我有来自cloud firestore的其他自定义数据,如何获取用户数据?_Javascript_Angular_Firebase_Ionic Framework_Google Cloud Firestore - Fatal编程技术网

Javascript 如果我有来自cloud firestore的其他自定义数据,如何获取用户数据?

Javascript 如果我有来自cloud firestore的其他自定义数据,如何获取用户数据?,javascript,angular,firebase,ionic-framework,google-cloud-firestore,Javascript,Angular,Firebase,Ionic Framework,Google Cloud Firestore,因此,我存储用户的电子邮件、姓名、年龄、电话号码和uid user.model.ts: export interface User { uid: string, name: string, email: string, phoneNumber: string, age: string } auth.service.ts: userData: any; constructor( private http: HttpClient,

因此,我存储用户的电子邮件、姓名、年龄、电话号码和uid

user.model.ts:

export interface User {
    uid: string,
    name: string,
    email: string,
    phoneNumber: string,
    age: string
}
auth.service.ts:

  userData: any;
  
  constructor(
    private http: HttpClient,
    private alertCtrl: AlertController,
    public afs: AngularFirestore,
    public afAuth: AngularFireAuth,
    public router: Router
    ) { 
      /* Saving user data in localstorage when 
      logged in and setting up null when logged out */
      this.afAuth.authState.subscribe( user => {
        if(user) {
          this.userData = user;
          localStorage.setItem('user', JSON.stringify(this.userData));
          JSON.parse(localStorage.getItem('user'));
        }else{
          localStorage.setItem('user', null);
          JSON.parse(localStorage.getItem('user'));
        }
      })
    }
    
    // Returns true when user is logged in (and email is verified)
    get isLoggedIn(): boolean {
      const user = JSON.parse(localStorage.getItem('user'));
      return (user !== null) ? true : false;
    }
    
    setUserData(user, name?, phoneNumber?, age?) {
      const userRef : AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
      const userData: User = {
        uid: user.uid,
        name,
        email: user.email,
        phoneNumber,
        age,
      }
      return userRef.set(userData, {
        merge: true
      })
    }
    
     // Sign up with email/password
    signup(name,  email, phoneNumber, age, password) {
      return this.afAuth.createUserWithEmailAndPassword(email, password)
        .then( (result) => {
          this.setUserData(result.user, name, phoneNumber, age);
          this.router.navigate(['/home']);
        }).catch((error) => {
          const code = error.code;
          let message = 'Could not sign up, please try again.';
          
          if(code === 'auth/email-already-in-use') {
            message = 'The email address exists already.';
          }else if(code === 'auth/invalid-password') {
            message = 'Password should be at least 6 characters';
          }else if(code === 'auth/invalid-email') {
            message = 'Invalid email.';
          }
          this.showAlert(message);
         })
    }
    
    // Sign in with email/password
    signIn(email, password) {
      return this.afAuth.signInWithEmailAndPassword(email, password)
        .then( (result) => {
          this.setUserData(result.user);
          this.router.navigate(['/home']);
        }).catch( (error) => {
          const code = error.code;
          let message = 'Could not sign up, please try again.';
          
          if(code === 'auth/invalid-email') {
            message = 'Invalid email.';
          }else if(code === 'auth/wrong-password') {
            message = 'Password is wrong.';
          }else if(code === 'auth/user-not-found') {
            message = 'User not found.';
          }
          this.showAlert(message);
        });
    }
userData:any;
建造师(
私有http:HttpClient,
私有alertCtrl:AlertController,
公共afs:AngularFirestore,
公共授权:AngularFireAuth,
公共路由器
) { 
/*在以下情况下将用户数据保存在localstorage中:
登录并在注销时设置null*/
this.afAuth.authState.subscribe(用户=>{
如果(用户){
this.userData=user;
setItem('user',JSON.stringify(this.userData));
parse(localStorage.getItem('user'));
}否则{
setItem('user',null);
parse(localStorage.getItem('user'));
}
})
}
//当用户登录(并验证电子邮件)时返回true
get isLoggedIn():布尔值{
const user=JSON.parse(localStorage.getItem('user'));
返回(用户!==null)?真:假;
}
setUserData(用户、姓名、电话号码、年龄){
const userRef:AngularFirestoreDocument=this.afs.doc(`users/${user.uid}`);
const userData:用户={
uid:user.uid,
名称
电子邮件:user.email,
电话号码,
年龄,
}
返回userRef.set(userData{
合并:对
})
}
//使用电子邮件/密码注册
注册(姓名、电子邮件、电话号码、年龄、密码){
返回此.afAuth.createUserWithEmailAndPassword(电子邮件,密码)
。然后((结果)=>{
this.setUserData(result.user、姓名、电话号码、年龄);
this.router.navigate(['/home']);
}).catch((错误)=>{
常量代码=error.code;
let message='无法注册,请重试';
如果(代码==='auth/email已在使用'){
message='电子邮件地址已存在';
}否则如果(代码=='auth/无效密码'){
消息='密码应至少为6个字符';
}否则,如果(代码=='auth/invalid email'){
消息='无效电子邮件';
}
此.showAlert(消息);
})
}
//使用电子邮件/密码登录
登录(电子邮件、密码){
使用email和password(电子邮件,密码)返回此.afAuth.signin
。然后((结果)=>{
this.setUserData(result.user);
this.router.navigate(['/home']);
}).catch((错误)=>{
常量代码=error.code;
let message='无法注册,请重试';
如果(代码=='auth/invalid email'){
消息='无效电子邮件';
}否则,如果(代码==='auth/密码错误'){
消息='密码错误';
}else if(代码===‘未找到身份验证/用户’){
消息='未找到用户';
}
此.showAlert(消息);
});
}
我想显示用户数据的home.page.html:

<ion-content>
     <!-- Show user data when logged in -->
     <div class="row" *ngIf="authService.userData as user">
      <div class="col-md-12">          
          <div class="media-body">
            <h1>Hello: <strong>{{user.name}}</strong></h1>
            <p>User ID: <strong>{{user.uid}}</strong></p>
            <p>Email: <strong>{{user.email}}</strong></p>
            <p>Phone number: <strong>{{user.phoneNumber}}</strong></p>
            <p>Age: <strong>{{user.age}}</strong></p>
          </div>
      </div>
    </div>
</ion-content>

你好:{{user.name}
用户ID:{{User.uid}

电子邮件:{{user.Email}}

电话号码:{{user.phoneNumber}

年龄:{{user.Age}}


数据被传递到数据库中。但问题是我不能显示像姓名、年龄、电话号码这样的自定义数据。只需显示电子邮件和uid。那有什么问题?如何显示来自cloud firestore的自定义数据?

是因为您没有定义名称、电话号码和年龄吗

#打字稿

    setUserData(user, name?, phoneNumber?, age?) {
      const userRef : AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
      const userData: User = {
        uid: user.uid,
        name: user.name,
        email: user.email,
        phoneNumber: user.phoneNumber,
        age: user.age,
      }
      return userRef.set(userData, {
        merge: true
      })
    }
setUserData(用户、姓名、电话号码、年龄){
const userRef:AngularFirestoreDocument=this.afs.doc(`users/${user.uid}`);
const userData:用户={
uid:user.uid,
name:user.name,
电子邮件:user.email,
phoneNumber:user.phoneNumber,
年龄:user.age,
}
返回userRef.set(userData{
合并:对
})
}
请让我知道它是否有效?我还在学习。多谢各位