[英]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
内的return
在forEach
内,将其移到外面,它应该可以工作
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.