【问题标题】:Get the user's key from the firebase database从 firebase 数据库中获取用户的密钥
【发布时间】:2017-07-15 20:50:31
【问题描述】:

我将用户数据存储在数据库中

users: {
  -Kp56mwwpFiCwIXLszqu: {
     email: "testemail@gmail.com",
     name: "test name",
     username: "testname"
  },
  -Kp58X2WGUVNazSSbRqF: {
     email: "anotheruser@gmail.com",
     name: "another user",
     username: "anotheruser"
  }
}

有没有办法获取用户的密钥,例如-Kp56mwwpFiCwIXLszqu 或 -Kp58X2WGUVNazSSbRqF。

我想向现有用户添加更多收藏。例如。我以testname 登录,我想从数据库中获取testname 密钥,以便在那里推送更多集合。

感谢任何帮助。谢谢

【问题讨论】:

    标签: angular firebase firebase-realtime-database angularfire2


    【解决方案1】:

    这些键看起来像自动生成的键。您可能希望通过将他的数据保存在他的uid 下来更改在数据库中保存用户配置文件的方式。

    例如:

    // Definition of the user profile class.
    export class UserProfile {
        email: string;
        name: string;
        username: string;
    }
    
    ----------------------------------------------------
    
    // Inside of some service.
    constructor(
        private angularFireAuth: AngularFireAuth,
        private angularFireDatabase: AngularFireDatabase
    ) { }
    
    saveUserProfile(profile: UserProfile): firebase.Promise<void> {
        let currentUserUid = this.angularFireAuth.auth.currentUser.uid;
        return angularFireDatabase.object(`users/${currentUserUid}`).update(profile);
    }
    

    这应该将用户配置文件数据保存在他的uid 下。请注意,我使用了update 方法而不是push,因为push 生成一个新的唯一键,这与update 或set 不同。然后,您的数据将具有如下结构:

    users: {
        {user_uid_will_be_here}: {
            email: 'someone@example.com',
            name: 'Test User',
            username: 'myusername'
        }
    }
    

    然后,当前登录的用户 uid 将来可以简单地访问该数据。

    getCurrentUserProfile(): UserProfile {
        let currentUserUid = this.angularFireAuth.auth.currentUser.uid;
        return this.angularFireDatabase.object(`users/${currentUserUid}`);
    }
    

    【讨论】:

    • 非常感谢......它工作得很好......我现在想知道为什么我没有考虑过...... :)
    • @surazzarus 没问题,很高兴我能帮上忙。 :)
    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多