Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Firebase';使用角度火更新对象属性值_Javascript_Angularjs_Firebase_Angularfire - Fatal编程技术网

Javascript 如何在Firebase';使用角度火更新对象属性值

Javascript 如何在Firebase';使用角度火更新对象属性值,javascript,angularjs,firebase,angularfire,Javascript,Angularjs,Firebase,Angularfire,我的代码如下: var FBref = new Firebase('https://employees.firebaseio.com/'); var peopleObject = $firebaseArray(FBref); var person = { "name":"John", "age":"20", "phone":"123-123-123" }; peopleObject.$add(pers

我的代码如下:

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);  
我使用上面的代码成功地添加了员工的新记录

我需要把约翰的电话号码更新到111-111-111。但是下面的代码不起作用

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

peopleObject.$save(person);

有人建议

当前,您正试图保存整个对象,但这不起作用

通过首先从db获取记录,然后更新该记录,首先检索旧记录。然后使用
$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
});

您可以使用以下代码获得someRecordKey
var someRecordKey;peopleObject.$add(person).then(函数(ref){//get新person的记录id someRecordKey=ref.key();})@huseyinozcan yes。。我们可以。。但我在考虑这件事可能会在不同的时段发生。。不是吗?谢谢你的提醒。。我更新了答案。:)很好的观点@PankajParkar!似乎更新操作发生在不同的环境中session@PankajParkar. 非常感谢。但是,我需要更新的记录不是我正在添加的新记录。这是一条已经在数据库中的记录。键(someRecordKey)用于我添加的新记录。如何获取firebase数据库中已有记录的密钥?