我試圖讓我的數組的長度,但我總是得到0,即使控制檯顯示,我把5個OBJETSArray.length等於0總是
async function getDiaries() {
return new Promise((resolve, reject) => {
var url = firebase.database().ref('/diary');
var diaries = [];
url.on('value', (snap) => {
snap.forEach((child) => {
diaries.push({
key: child.key,
name: child.val().name,
date: child.val().description,
url: child.val().url,
idOwner: child.val().idOwner
});
});
});
console.log(diaries.length);// get 0
resolve(diaries)
})}
在您的數組被填充之前正在打印...您在異步概念中誤導 – guijob