我有用户数据存储在数据库中
users: {
-Kp56mwwpFiCwIXLszqu: {
email: "[email protected]",
name: "test name",
username: "testname"
},
-Kp58X2WGUVNazSSbRqF: {
email: "[email protected]",
name: "another user",
username: "anotheruser"
}
}
有没有办法获取用户的密钥,例如-Kp56mwwpFiCwIXLszqu
或-Kp58X2WGUVNazSSbRqF
.
我想向现有用户添加更多集合。例如。我以 a 身份登录,testname
我想testname
从数据库中获取密钥,以便我可以在那里推送更多集合。
任何帮助表示赞赏。谢谢
这些密钥看起来像自动生成的密钥。您可能希望通过将他的数据保存在他的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
or不同的新唯一键set
。您的数据将具有如下结构:
users: {
{user_uid_will_be_here}: {
email: '[email protected]',
name: 'Test User',
username: 'myusername'
}
}
然后,您可以在将来由当前登录的用户轻松访问该数据uid
。
getCurrentUserProfile(): UserProfile {
let currentUserUid = this.angularFireAuth.auth.currentUser.uid;
return this.angularFireDatabase.object(`users/${currentUserUid}`);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句