Firebase - 如何更新 Firebase 实时数据库中的对象

Firebase - How can I update an object in the Firebase Realtime Database

如何更新现有对象。

我在更新 Firebase 实时数据库中的对象时遇到问题。

有人知道怎么做吗?

我正在使用:Angular 和 TypeScript。

这是我当前的 TypeScript 代码:

updateProfile = (profile): any => {
  return new Promise((resolve, reject) => { 
    var refProfile = new Firebase("https://poolcover.firebaseio.com/profiles");

    refProfile.update(profile, function(error) {
      if (error) {
        console.log('Synchronization failed');
        resolve(false); 
      } else {
        console.log('Synchronization succeeded');
        resolve(true); 
      }
    });
  })  
}

这是我要更新的数据库 table 和配置文件:

您需要找到配置文件的密钥。存储用户数据时,您应该关闭 uid 而不是带有 .push().

的 id

此外,不要使用已完成的回调,而是使用侦听器。

完成的回调等待服务器响应,其中侦听器首先在本地触发。所以侦听器回调更快。

constructor() {
  this.profileRef = new Firebase("<my-firebase-app>/profiles");
  var currentUser = this.profileRef.getAuth(); // if authenticated 
  this.userRef = refProfile.child(currentUser.uid);
  userRef.on('value', (snap) => {
    console.log('Profile updated');
    console.log(snap.val()); // update profile info
  });
}

updateProfile(profile) {
  this.userRef.update(profile);
}