简体   繁体   中英

create-react-app imported function returns undefined

I have 2 filter functions exported from helpers.js file:

    export const filterSource = (data, source_tags_array) => {
    let accumulator = []
     source_tags_array.forEach( tag => {
         accumulator = data.filter( event => {return event.source_type.includes(tag)})
         // accumulator.concat(...tmp)
     })
    accumulator.sort(function(a,b) {
        return new Date(b.date) - new Date(a.date)
    })
    return accumulator

}


export const filterTags = (data, tags_array) => {
    let accumulator = []
    tags_array.forEach( tag => {
        data.forEach((event) => {
            if (!accumulator.some(el => el.id === event.id) && event.tags.includes(tag)) {
                accumulator.push(event)
            }
        })
      return accumulator
    })
}

And I import them in reducer file:

import * as helpers from "../helpers";

export const filter = events_data => {
    return (dispatch, getState) => {

        const { access_tags, access_source_tags} = getState().user
        const filteredSource = helpers.filterSource(events_data, access_source_tags)
        const events =  helpers.filterTags(filteredSource, access_tags)
        dispatch(fetchEventsSuccess(events))
    }}

While first one (filterSource) works fine the second (filterTags) is undeifned

Screenshot with variables values from debuger

带有来自调试器的变量值的屏幕截图

In case you cannot see image... filteredSource: Array(160) events_data: Array(395) accesss_source_tags: Array(1) access_tags: Array(1) events: UNDEFINED

The accumulator from filterTags in helpers.js on return point is Array(65) but for some reason after import is undefined ...

I tried named imports as well... same problem.

Any ideas?

The return inside filterTags is inside the forEach , move it outside and it should work

export const filterTags = (data, tags_array) => {
    let accumulator = []
    tags_array.forEach( tag => {
        data.forEach((event) => {
            if (!accumulator.some(el => el.id === event.id) && event.tags.includes(tag)) {
                accumulator.push(event)
            }
        })
    })
    return accumulator
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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