2015-12-13 66 views
0

Firebase - 如何更新现有对象。Firebase - 如何更新Firebase json表对象中的现有对象

我在更新Firebase中的对象时遇到问题。

有谁知道如何做到这一点?

我正在使用的语言是:firebase,angular2和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); 
         } 
       }); 
      }) 
    } 

这是数据库表和配置文件我在寻找更新:

enter image description here

回答

0

你需要找到关键配置文件。存储用户数据时,您应该键入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); 
} 
+0

谢谢大卫我今天会试试这个 – AngularM

+0

如果上面的代码你写的getUser(); 你的意思是getAuth();? – AngularM

+0

是:)感谢您的支持 –