使用$优先级和$保存覆盖对象火力地堡AngularFire [英] Firebase AngularFire using $priority and the $save overwrites the object
问题描述
根据在 AngularFire文档我试图设置 $优先
的对象上,然后用 $保存()
来更新火力地堡的数据。结果
结果我得到的反而是越来越对象覆盖为空(在火力地堡它的条目)。
这里是我的code
REF VAR =新火力地堡(firebaseListPath);
VAR myList中= $火力点(REF)$ asArray()。 myList中。$添加({
创建:Firebase.ServerValue.TIMESTAMP,
用户:用户id
}),然后(功能(newItemRef){
VAR newItemObj = $火力(newItemRef)$ asObject()。
。newItemObj $ =优先级用户id;
。newItemObj $保存();
});
尝试直接传递优先到$添加:
myList中。$加({
创建:Firebase.ServerValue.TIMESTAMP,
用户:用户ID
$优先级:用户id
});
有也没有理由骂$ asArray和$ asObject在相同的同步数据(它已经同步)。这会在当前的模型工作,但是是不必要的:
myList中。$加({
创建:Firebase.ServerValue.TIMESTAMP,
用户:用户id
}),然后(功能(newItemRef){
VAR项目= myList上$ getRecord(newItemRef.name());
。项目$ =优先级用户id;
。myList上$保存(项目);
});
不过,实在没有理由在这里使用AngularFire。如果不绑定任何的观点,那么你可以简单地拨打电话直接向裁判:
REF VAR =新火力地堡(firebaseListPath);
。ref.push()setWithPriority({...},用户id);
Based on the AngularFire documentation I'm trying to set $priority
on an object, then use $save()
to update the data on Firebase.
The result I get instead is the object getting overwritten as empty (it's entry in Firebase).
Here is my code
var ref = new Firebase(firebaseListPath);
var myList = $firebase(ref).$asArray();
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId
}).then(function(newItemRef){
var newItemObj = $firebase(newItemRef).$asObject();
newItemObj.$priority = userId;
newItemObj .$save();
});
Try passing the priority directly into $add:
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId,
$priority: userId
});
There is also no reason to call $asArray and $asObject on the same synchronized data (it's already synchronized). This would work in your current model, but be unnecessary:
myList.$add({
created: Firebase.ServerValue.TIMESTAMP,
user: userId
}).then(function(newItemRef){
var item = myList.$getRecord(newItemRef.name());
item.$priority = userId;
myList.$save(item);
});
However, there is really no reason to be using AngularFire here. If you aren't binding anything to the view, then you can simply make the call directly to the ref:
var ref = new Firebase(firebaseListPath);
ref.push().setWithPriority({ ... }, userId);
这篇关于使用$优先级和$保存覆盖对象火力地堡AngularFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!