编辑:似乎value [i] .extrainfoimage仅在imageRef.child中未定义(" -JlSvEAw ......
到目前为止,我已经阅读了两次文档,但仍然没有能够解决这个问题。
基本上我从firebase加载一些数据并将其设置为数组。我遍历那个数组并打算用我从Firebase数据库中的其他位置获取的数据替换一些属性。但是每次我去替换我得到的属性
"未捕获的TypeError:无法设置属性' extrainfoimage'未定义"
这是我的代码:
var questionsRef = new Firebase("https://XX.firebaseio.com/Questions");
var imageRef = new Firebase("https://XX.firebaseio.com/Images");
//get the first 6 items
var query = questionsRef.orderByChild('date_time_asked').limitToFirst(6);
//get them as a firebase array promise
$scope.questions = $firebaseArray(query);
//wait for it to load
$scope.questions.$loaded().then(function (value) {
//iterate through
for (i = 0; i < value.length; i++) {
//check if there is data to be replaced
if (value[i].extrainfoimage) {
//if there is fetch it from firebase and replace
imageRef.child("-JlSvEAwu5-WZJkOE_b/image").once("value",function(data){
value[i].extrainfoimage = data.val();
});
}
}
})
答案 0 :(得分:1)
可能是因为价值中的最后一项没有extrainfoimage
。因为value[i].extrainfoimage
的集合是异步的,所以它不会捕获正确的i
值,因此在执行时会失败。
尝试将其包装在IIFE中:
for (i = 0; i < value.length; i++) {
//check if there is data to be replaced
if (value[i].extrainfoimage) {
(function(curr) {
//if there is fetch it from firebase and replace
imageRef.child("-JlSvEAwu5-WZJkOE_b/image").once("value",function(data){
value[curr].extrainfoimage = data.val();
});
})(i);
}
}