我目前正在构建一个聊天应用程序,其中包含配置文件图片(存储在firebase存储中)以及用户数据和文本(存储在firebase数据库中) . 我试图遍历消息数据库并显示消息数据以及旁边的用户的 Profiles 图片 . 我目前正在使用以下代码:
var theDataRef = new Firebase('https://my-chat.firebaseio.com');
theDataRef.orderByChild("timestamp").limitToLast(25).once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var message = childSnapshot.val();
var email = message.email;
var stringEmail = email.split('.').join('-');
var storage = firebase.storage();
var storageRef = storage.ref();
var starsRef = storageRef.child('images/' + stringEmail + '.jpg');
starsRef.getDownloadURL().then(function (url) {
displaytableRow(message.name, message.text, url);
}).catch(function (error) {
displaytableRow(message.name, message.text, "images/dog.jpg");
});
});
});
代码在设备上正常运行,但它只能工作一半的时间 . 我认为这与数据库调用的速度比从存储中检索图像要快得多 .
我的问题是,这是在数据库调用中迭代从存储中获取图像的最佳方法,而且如果数据库调用太快就会出现问题,我怎样才能使这个函数等待图像下载后再移动到下一个记录?