问题描述
我想提出一个web应用程序,允许用户种植一棵树。当用户受精树,属性肥应该增加。但是,应用程序不断给我的错误,因为它已决定,肥是不确定的。
I am making a web app that allows the user to grow a tree. When the user fertilizes the tree, the attribute "fertilizer" should increment. However, the app keeps giving me the error because it has decided that "fertilizer" is undefined.
更新得分函数如下:
function updateScore(){
if (usr!=null){
var tree = $firebase(new Firebase("https://myapp.firebaseio.com/trees/"+usr.treeid));
var usrTree = tree.$asObject();
var data = null;
if (typeof usrTree.data !== 'undefined'){
data = usrTree.data;
}
console.log(usrTree);
console.log(usrTree.$id);
console.log(usrTree.uid);
console.log(usrTree.fertilizer);
tree.$update({fertilizer: usrTree.fertilizer+1});
}
}
}
所以,控制台,这些结果:
So the console gives these results:
e {$$conf: Object, $id: "-JcDm0UlYVGFfFAX0GSf", $priority: null, $save: function, $remove: function…}
$$conf: Object
$id: "-JcDm0UlYVGFfFAX0GSf"
$priority: null
fertilizer: 0
level: 0
pesticide: 0
sunshine: 0
uid: "simplelogin:6"
water: 0
__proto__: Object
-JcDm0UlYVGFfFAX0GSf
undefined
undefined
我认为第一个是 usrTree
对象,第二个是的$ id
和第三,第四应 UID
和化肥
,但都陷入不确定。然而,在 usrTree
对象, UID
和化肥
做显示为定义,他们火力锻造存在:
I believe the first one is the usrTree
object, the second one is the $id
and the third and the fourth should be uid
and fertilizer
but are caught undefined. However, in the usrTree
object, uid
and fertilizer
do show up as defined and they exist in firebase forge:
我在做什么错在这里?我应该怎么做来更新数据??
What am I doing wrong here?? What should I do to update the data??
推荐答案
事实证明,我需要使用$加载。我改变了code以下内容和它的作品了。
It turns out that I need to use $loaded. I changed the code to the following and it works now.
var tree = $firebase(FirebaseRef.child("trees").child(usr.treeid));
var usrTree = tree.$asObject();
usrTree.$loaded().then(function() {
tree.$update({fertilizer: usrTree.fertilizer + 1});
}
这篇关于错误:Firebase.update失败:第一个参数包含楠财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!