Task Queue Error kiedy używam Promisów

0

Siema mam taki problem. Pobieram dane z cache który jest nakładką na AsyncStorage.

static getDiscounts(categoryAndPage) {
        return AsyncStorage.getItem(categoryAndPage);
    }

Następnie pobieram sobie te dane i aktualizuję stan poprzez this.set.State()

        var discountsPromise = DiscountsCache.getDiscounts(discountCacheKey);

        discountsPromise.then(cachedDiscounts => {
            
            if (cachedDiscounts !== null) {
                var result = JSON.parse(cachedDiscounts);
                this.setState({
                    discounts: page === 1
                        ? result
                        : [
                            ...this.state.discounts,
                            ...result
                        ]
                                    })
            } else {... tutaj pobieram dane requestem i przypisuje do cache

Na tą zmianę stanu reaguje component w metodzie render

 render() {
        return (
                    ....
                        <FlatList
                            data={this.state.discounts}
                            renderItem={({item}) => {
                            var media = item._embedded["wp:featuredmedia"][0]; <-- tutaj dostaję błąd

Błąd

TaskQueue: Error with task : Cannot read property 'wp:featuredmedia' of undefined

Dodam tylko że bez cache, kiedy za każdym razem pobieram dane z API, wszystko działa poprawnie.

EDIT: dostaję jeszcze warning przed wyjątkiem

Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'wp:featuredmedia' of undefined

1

Cannot read property wp:featuredmedia of undefined
=> item._embedded jest undefined =>
=> czyli item nie ma property _embedded =>
=> czyli item przekazywany przy wywołaniu renderItem z poziomu FlatList nie ma property _embedded
( => czyli - domyślam się - this.state.discounts zawiera elementy, które nie mają property _embedded - chyba, ze to nie to renderuje renderItem)

Teraz musisz zbadać, czemu jej nie ma.

1 użytkowników online, w tym zalogowanych: 0, gości: 1