【问题标题】:Firebase - How can I update an object in the Firebase Realtime DatabaseFirebase - 如何更新 Firebase 实时数据库中的对象
【发布时间】:2019-06-23 10:28:22
【问题描述】:

如何更新现有对象。

我在更新 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); 
      }
    });
  })  
}

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

【问题讨论】:

    标签: angular typescript firebase-realtime-database


    【解决方案1】:

    您需要找到配置文件的密钥。存储用户数据时,您应该关闭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);
    }
    

    【讨论】:

    • 谢谢大卫,我今天会试试这个
    • 如果上面的代码你写的getUser();你是说 getAuth(); 吗?
    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2021-04-07
    相关资源
    最近更新 更多