从Firebase数据库获取一些数据并将其返回到页面上时,我遇到了一个问题。
这是数据检索功能:
let appData = function() {
let read = async function (dataset) {
let dataReference;
dataReference = firebase.database().ref(dataset + '/');
let itemDataArray = [];
dataReference.orderByChild('created').limitToLast(100).on('value', function (response) {
response.forEach(function (element) {
let itemData = element.val();
if (dataset == 'blog') {
itemDataArray.push({
id: element.key,
author: itemData.author,
content: itemData.content,
created: itemData.created,
target: itemData.target || '_top',
title: itemData.title,
url: itemData.url
});
}
else if (dataset == 'pages')
{
itemDataArray.push({
id: element.key,
content: itemData.content,
created: itemData.created,
deprecated: itemData.deprecated || false,
menuText: itemData.menuText,
modified: itemData.modified,
parent: itemData.parent,
path: itemData.path || '/',
repo: itemData.repo,
sortOrder: itemData.sortOrder || 0,
target: itemData.target || '_top',
title: itemData.title,
tooltip: itemData.tooltip
});
}
});
});
return await itemDataArray.reverse();
}
return {
read: read
}
}();
这是我的调用函数(带有失败原因的注释):
function renderBlog() {
appData.read('blog').then(function(result) {
console.log(result); // This writes a value...
let i = 0;
$.each(result, function( key, value ){
console.log(result[i].created); // Nothing ever happens here...
i++;
});
document.getElementById('Content').innerHTML = 'Done...';
});
}