我如何在 Firebase 中使用 $save()' Angular-Fire 来更新对象 属性 值

How do I use $save() in Firebase' Angular-Fire to Update Object Property Values

我有以下代码:

var FBref = new Firebase('https://employees.firebaseio.com/');
var peopleObject = $firebaseArray(FBref);

var person = {  
            "name":"John",
            "age":"20",
            "phone":"123-123-123"
        };

peopleObject.$add(person);  

我使用上面的代码成功添加了员工的新记录。

我需要将约翰的 phone 号码更新为 111-111-111。但是下面的代码不起作用。

var person = {  
            "name":"John",
            "age":"20",
            "phone":"111-111-111" //New phone number
        };

peopleObject.$save(person);

有人指教

目前您正在尝试保存整个对象,但这是行不通的。

首先从数据库中获取记录,然后更新该记录,从而先检索旧记录。此后使用 $save 方法保存该对象。

//if update action is happening is similar session you can get unique by below code
var someRecordKey; 
peopleObject.$add(person).then(function(ref) { 
    //get record id of new person 
    someRecordKey = ref.key(); 
});

// get record by unique key
var person = peopleObject.$getRecord(someRecordKey);
// change a message and save it
item.phone = "111-111-111";
peopleObject.$save(person).then(function() {
    // data has been saved to our database
});