Angularfire2 更新记录
Angularfire2 Update a Record
我是 angular 的新手,我正在构建一个小型网络应用程序。
我已成功设置 angularfire 2 并使用 Push() 成功创建了一个新的列表条目。
假设我的列表(用户)包含用户电子邮件和名字。只有电子邮件是通过 Push 方法添加的,我想通过添加名字来更新该记录。
我参考了 Angularfire2 文档并设法做到了这一点:
usrEmail: string;
usrData: AngularFireList<any>;
usr: Observable<any>;
constructor( private authService: AuthService, private afd: AngularFireDatabase) {
this.usrData = this.afd.list('/users')
// now get the current user
this.usr = this.usrData.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
}
我不确定我应该如何通过 usrEmail 过滤列表并获取更新记录的密钥。
非常感谢为我指明正确方向的任何帮助。
您使用的查询获取所有 /users
而不仅仅是具有匹配电子邮件地址的单个用户。
我假定您可以访问该电子邮件地址并将其分配给一个变量 userEmailAddress
。
this.usr = this.afd.list('/users', ref => ref
.orderByChild('usrEmail')
.equalTo(userEmailAddress)
)
.snapshotChanges()
.map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
})
.first()
.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log('Snapshot Key: ', snapshot.key);
console.log('Snapshot Payload: ', snapshot.val());
});
});
snapshot.key
的输出将是您要获取的密钥。
但是在您的场景中,会有更好的方法来存储客户的电子邮件地址。您可以在数据库中创建一个包含用户 ID 的路径,例如;
我假设您可以通过 authService 访问 uid。 this.authService.uid
this.afd.object(`/users/${this.authService.uid}`).update({
email: userEmailAddress
});
用户的 uid
永远不会改变,并且它是该用户唯一的,因此您可以用它创建数据库路径以便于访问。
我是 angular 的新手,我正在构建一个小型网络应用程序。
我已成功设置 angularfire 2 并使用 Push() 成功创建了一个新的列表条目。
假设我的列表(用户)包含用户电子邮件和名字。只有电子邮件是通过 Push 方法添加的,我想通过添加名字来更新该记录。
我参考了 Angularfire2 文档并设法做到了这一点:
usrEmail: string;
usrData: AngularFireList<any>;
usr: Observable<any>;
constructor( private authService: AuthService, private afd: AngularFireDatabase) {
this.usrData = this.afd.list('/users')
// now get the current user
this.usr = this.usrData.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
}
我不确定我应该如何通过 usrEmail 过滤列表并获取更新记录的密钥。
非常感谢为我指明正确方向的任何帮助。
您使用的查询获取所有 /users
而不仅仅是具有匹配电子邮件地址的单个用户。
我假定您可以访问该电子邮件地址并将其分配给一个变量 userEmailAddress
。
this.usr = this.afd.list('/users', ref => ref
.orderByChild('usrEmail')
.equalTo(userEmailAddress)
)
.snapshotChanges()
.map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
})
.first()
.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log('Snapshot Key: ', snapshot.key);
console.log('Snapshot Payload: ', snapshot.val());
});
});
snapshot.key
的输出将是您要获取的密钥。
但是在您的场景中,会有更好的方法来存储客户的电子邮件地址。您可以在数据库中创建一个包含用户 ID 的路径,例如;
我假设您可以通过 authService 访问 uid。 this.authService.uid
this.afd.object(`/users/${this.authService.uid}`).update({
email: userEmailAddress
});
用户的 uid
永远不会改变,并且它是该用户唯一的,因此您可以用它创建数据库路径以便于访问。