简体   繁体   English

create-react-app 导入的 function 返回 undefined

[英]create-react-app imported function returns undefined

I have 2 filter functions exported from helpers.js file:我有 2 个从 helpers.js 文件导出的过滤器函数:

    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:我将它们导入reducer文件:

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虽然第一个(filterSource)工作正常,但第二个(filterTags)是未定义

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如果您看不到图像...过滤源:数组(160)事件数据:数组(395)访问源标签:数组(1)访问标签:数组(1)事件:未定义

The accumulator from filterTags in helpers.js on return point is Array(65) but for some reason after import is undefined ...返回点上 helpers.js 中 filterTags 的累加器Array(65)但由于某种原因,导入后未定义...

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 filterTags内的returnforEach内,将其移到外面,它应该可以工作

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
}

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

相关问题 Github页面上的Create-React-App返回一个空白页面 - Create-React-App on Github-pages returns a blank page Create-react-app registerServiceWorker在getRegistration()承诺中提供未定义 - Create-react-app registerServiceWorker giving undefined at getRegistration() promise 导入的函数在 React 中未定义 - Imported function is undefined in React 带有 Create-react-app 的 Cordova - Cordova with Create-react-app Node + create-react-app + Express - 如何将函数导入 Express - Node + create-react-app + Express - how to import function into Express list.map 不是 function 帮助 create-react-app - list.map is not a function help create-react-app TS 编译器/create-react-app 从函数中删除异步 - TS compiler/create-react-app removes async from function process.env 变量未定义 React App - 不使用 create-react-app - process.env variables undefined React App - Not using create-react-app 无法读取未定义的属性“历史记录”-react-router v4和create-react-app - Cannot read property 'history' of undefined — react-router v4 & create-react-app TypeError:无法解构“对象(…)(…)”的属性“setValues”,因为它未定义。 (反应/创建反应应用) - TypeError: Cannot destructure property 'setValues' of 'Object(…)(…)' as it is undefined. (react / create-react-app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM