Firebase 推送 - 删除唯一对象并插入新对象(基本上覆盖内容)
Firebase push - deletes unique object and inserts new one (Overwrites content basically)
我有一个 运行 firebase 应用程序。当我尝试使用 push() 方法时,它基本上覆盖了现有的 JSON。这是一个例子:
第一次,生成以下 JSON:
JSON
"deviceIDs" : {
"-JzCx5C_13eoXPEgklMW" : {
"author" : "gracehop22",
"deviceID" : "99alpha",
"title" : "Announcing COBOL, a New Programming Language"
}
}
下一次,如果我调用相同的函数,上面的 JSON 会被删除并插入一个新的 JSON,例如这个:
JSON
"deviceIDs" : {
"-JzCxbuEj2V1kmvvgqnc" : {
"author" : "gracehop22",
"deviceID" : "99alpha",
"title" : "Announcing COBOL, a New Programming Language"
}
}
这是我的代码片段:
function CreateUserProfile(UID, name, email, deviceID) {
var ref = new Firebase($scope.firebaseurl + '/' + UID);
var profileArray = {UserProfile:{}};
profileArray.UserProfile.UID = UID;
profileArray.UserProfile.name = name;
profileArray.UserProfile.email = email;
profileArray.UserProfile.deviceID = deviceID;
var onComplete = function (error) {
if (error) {
console.log('Synchronization failed');
} else {
//1. On Success, Store Key User Profile Elements
localStorage.setItem("kasimaProfileInfo",JSON.stringify(profileArray));
$rootScope.username = name;
//2. Hide the feedback and change screens
$timeout(function () {
$scope.HideFeedback();
$scope.ChangeLoc('/featured');
}, 1500);
}
};
ref.set(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
var newPostRef = postsRef.push();
newPostRef.set({
deviceID: deviceID,
author: "gracehop22",
title: "Announcing COBOL, a New Programming Language"
});
}
当您设置 profileArray
:
时,您将覆盖整个参考
...
ref.set(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
...
你可能想在此处使用 update()
:
...
ref.update(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
...
更新
Firebase update()
函数设置您传递给它的 JSON 对象中的属性值。因此,您的新 profileArray.UserProfile
将替换现有数据。
解决方法是不在本地构建嵌套的JSON结构,而是在需要更新的下层位置更新数据:
ref.child('UserProfile').update(profileArray.UserProfile, onComplete);
这消除了对 profileArray
:
的全部需求
var userProfile = {
UID: UID,
name: name,
email: email,
decideID: deviceID
};
ref.child('UserProfile').update(userProfile, onComplete);
有关工作示例,请参阅:http://jsbin.com/ciqoge/edit?js,console
下一次:如果您直接提供这样的jsbin/jsfiddle,将更容易快速帮助您。
我有一个 运行 firebase 应用程序。当我尝试使用 push() 方法时,它基本上覆盖了现有的 JSON。这是一个例子: 第一次,生成以下 JSON:
JSON
"deviceIDs" : {
"-JzCx5C_13eoXPEgklMW" : {
"author" : "gracehop22",
"deviceID" : "99alpha",
"title" : "Announcing COBOL, a New Programming Language"
}
}
下一次,如果我调用相同的函数,上面的 JSON 会被删除并插入一个新的 JSON,例如这个:
JSON
"deviceIDs" : {
"-JzCxbuEj2V1kmvvgqnc" : {
"author" : "gracehop22",
"deviceID" : "99alpha",
"title" : "Announcing COBOL, a New Programming Language"
}
}
这是我的代码片段:
function CreateUserProfile(UID, name, email, deviceID) {
var ref = new Firebase($scope.firebaseurl + '/' + UID);
var profileArray = {UserProfile:{}};
profileArray.UserProfile.UID = UID;
profileArray.UserProfile.name = name;
profileArray.UserProfile.email = email;
profileArray.UserProfile.deviceID = deviceID;
var onComplete = function (error) {
if (error) {
console.log('Synchronization failed');
} else {
//1. On Success, Store Key User Profile Elements
localStorage.setItem("kasimaProfileInfo",JSON.stringify(profileArray));
$rootScope.username = name;
//2. Hide the feedback and change screens
$timeout(function () {
$scope.HideFeedback();
$scope.ChangeLoc('/featured');
}, 1500);
}
};
ref.set(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
var newPostRef = postsRef.push();
newPostRef.set({
deviceID: deviceID,
author: "gracehop22",
title: "Announcing COBOL, a New Programming Language"
});
}
当您设置 profileArray
:
...
ref.set(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
...
你可能想在此处使用 update()
:
...
ref.update(profileArray, onComplete);
var postsRef = ref.child("deviceIDs");
...
更新
Firebase update()
函数设置您传递给它的 JSON 对象中的属性值。因此,您的新 profileArray.UserProfile
将替换现有数据。
解决方法是不在本地构建嵌套的JSON结构,而是在需要更新的下层位置更新数据:
ref.child('UserProfile').update(profileArray.UserProfile, onComplete);
这消除了对 profileArray
:
var userProfile = {
UID: UID,
name: name,
email: email,
decideID: deviceID
};
ref.child('UserProfile').update(userProfile, onComplete);
有关工作示例,请参阅:http://jsbin.com/ciqoge/edit?js,console
下一次:如果您直接提供这样的jsbin/jsfiddle,将更容易快速帮助您。