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

Get UID and insert in object in database firebase Angular 6

我需要获取用户在身份验证中创建的 UID,并将此 UID 插入到数据库 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())
   })
}

如果我没有理解错你的问题。

就像这里提到的那样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。

现在将其插入您的 table:

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文件table.

我在我的代码中这样做了,在你的代码中使用了 base 朋友并为我工作谢谢朋友

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);
  });
}