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

时间:2016-04-20 07:05:36

标签: javascript angularjs firebase angularfire

我有以下代码:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.test.test"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
}

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

我需要将John的电话号码更新为111-111-111。但是下面的代码不起作用。

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);  

有人建议

1 个答案:

答案 0 :(得分:2)

目前您正在尝试保存整个对象,但这不起作用。

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