获取UID并插入数据库firebase Angular 6中的对象

时间:2018-05-22 13:40:33

标签: firebase angular6

我需要获取用户在身份验证中创建的UID,并将此UID插入在Database Firebase中创建的项目中,我在auth.ts中创建用户

createUser(user: any, path: string) {
return this.afu.auth.createUserWithEmailAndPassword(user.email, user.password)
                            .then((res) => { 
                              let id = res.uid;
                              this.teste(id, path);
                              return this.service.save(user, path)
                            });
}

在这个服务中我将发送到另一个服务来创建一个对象关系,现在我的userService.ts,那么如何获取这个UID并插入user / id属性?

save(user: any, path: string) {
return new Promise((resolve, reject) => {
    return this.db.list(this.PATH + path)
            .push(user)
            .then(() => resolve())
   })
}

enter image description here

2 个答案:

答案 0 :(得分:1)

如果我希望我不明白你的问题。

像这里提到的https://firebase.google.com/docs/auth/web/manage-users

var user = firebase.auth().currentUser;
var name, email, photoUrl, uid, emailVerified;

    if (user != null) {
      name = user.displayName;
      email = user.email;
      photoUrl = user.photoURL;
      emailVerified = user.emailVerified;
      uid = user.uid; 
    }

现在您已获得当前用户的ID。

现在将其插入您的表格:

ref.child("something"+count).set({
  uid: this.uid,
  email: this.email,
  //and more
});

count代表对象的id,例如:

{
  "0": {
      "uid": "22",
      "email": "something@gmail.com"
    },
   "1": {
      "date_of_birth": "33",
      "full_name": "anothersomething@gmail.com"
    }
} 
当你要求表格时,

是你对应的json文件。

答案 1 :(得分:0)

我在我的代码中以代码朋友为基础这样做,并感谢朋友

createUser( user: any, path: string): Promise<any> {
return firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
  .then( newUser => {
    firebase.database()
    .ref(`/users/${path}/`)
    .child(newUser.uid)
    .set(user);
  });
}