简体   繁体   English

如何从Firebase存储中获取react.js的异步图像数据?

[英]How get async image data for react.js from firebase storage?

Please tell, how to put in queue return array with data. 请告诉我们如何将队列返回数组与数据一起放入。 Because component render faster than firebase do send data. 因为组件渲染的速度比firebase的传输速度快。

This code fragment from action (redux's) 此代码片段来自动作(redux)

function fetchPosts(key) {
return dispatch => {
    dispatch(requestPosts(key))
    return database.ref('article/').once('value', snap => {
        let childData = []
        snap.forEach(function(child) {
            getImage(child.val().thumb).then((url) => {
                console.log(url)
                return {
                    "id": child.key,
                    "url": url,
                    ...child.val()
                }
            }).then((array) => {
                console.log(array)
                childData.push(array)
            })
        })
        dispatch(receivePosts(key, childData))
    })
    .catch((error) => {
      console.log(error)
      dispatch(receivePosts(error))
    })
}

} }

You need to wait for all snap responses, like this: 您需要等待所有快照响应,如下所示:

 // Boilerplate start --- function getImage(thumb) { return new Promise((resolve) => { setTimeout(() => resolve(thumb + ' url'), Math.random() * 500); }); } function Child(key, value) { this.key = key; this.value = value; } Child.prototype.val = function () { return this.value; } function receivePosts(key, childData) { return {key, childData}; } function dispatch(action) { console.log(action); } snap = [ new Child('key1', {thumb: 'thumb1'}), new Child('key2', {thumb: 'thumb2'}), new Child('key3', {thumb: 'thumb3'}), new Child('key4', {thumb: 'thumb4'}), new Child('key5', {thumb: 'thumb5'}), new Child('key6', {thumb: 'thumb6'}), new Child('key7', {thumb: 'thumb7'}), new Child('key8', {thumb: 'thumb8'}) ]; // Boilerplate end --- function insideDatabaseRefOnce(key) { return Promise.all(snap.map(function(child) { return getImage(child.val().thumb).then((url) => { return { "id": child.key, "url": url, ...child.val() } }); })).then((childData) => { dispatch(receivePosts(key, childData)) }); } insideDatabaseRefOnce('The key'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM