我正在构建一个React Redux费用跟踪应用程序,并使用moment.js和react-dates设置时间和过滤时间范围。但是,当我选择任何位置或使用“ x”清除日期,然后屏幕上都没有任何内容时,出现错误。
expenses.js:9 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
at expenses.js:9
at Array.filter (<anonymous>)
at expenses.js:5
at Function.mapStateToProps [as mapToProps] (ExpenseList.js:17)
at mapToPropsProxy (wrapMapToProps.js:41)
at handleNewState (selectorFactory.js:44)
at handleSubsequentCalls (selectorFactory.js:58)
at pureFinalPropsSelector (selectorFactory.js:63)
at Subscription.checkForUpdates [as onStateChange] (connectAdvanced.js:247)
at Subscription.handleChangeWrapper (Subscription.js:70)
我已经仔细检查过,但仍然找不到问题所在...
减速器/过滤器
const filterReducerDefaultState = {
text: '',
sortBy: 'date',
startDate: moment().startOf('month'),
endDate: moment().endOf('month')
}
这是选择器/expenses.js:
import moment from 'moment'
//Get visible expenses
export default (expenses, { text, sortBy, startDate, endDate }) => {
return expenses.filter((expense) => {
const createdAtMoment = moment(expense.createdAt)
const startDateMatch = startDate ? startDate.isSameOrBefore(createdAtMoment, 'day') : true
const endDateMatch = endDate? endDate.isSameOrAfter(createdAtMoment, 'day'): true
const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())
return startDateMatch && endDateMatch && textMatch
}).sort((a, b) => {
if (sortBy === 'date') {
return a.createdAt < b.createdAt ? 1 : -1
} else if (sortBy === 'amount') {
return a.amount < b.amount ? 1 : -1
}
})
}
这是components / ExpenseListFilters.js:
import React from 'react'
import { connect } from 'react-redux'
import { DateRangePicker } from 'react-dates'
import { setTextFilter, sortByAmount, sortByDate, setStartDate, setEndDate } from '../actions/filters'
class ExpenseListFilters extends React.Component{
state={
calendarFocused: null
}
onDatesChange = ({startDate, endDate}) => {
this.props.dispatch(setStartDate(startDate))
this.props.dispatch(setEndDate(endDate))
}
onFocusChange = (calendarFocused) => {
this.setState(() => ({ calendarFocused}))
}
render(){
return (
<div>
<input
type="text"
value={this.props.filters.text}
onChange={(e) => {
this.props.dispatch(setTextFilter(e.target.value))
}} />
<select
value ={this.props.filters.sortBy}
onChange={(e)=> {
if (e.target.value === 'date'){
this.props.dispatch(sortByDate())
} else if (e.target.value === 'amount') {
this.props.dispatch(sortByAmount())
}
}}>
<option value="date">Date</option>
<option value="amount">Amount</option>
</select>
<DateRangePicker
startDate={this.props.filters.startDate}
startDateId='startDateId'
endDate={this.props.filters.endDate}
endDateId='endDateId'
onDatesChange={this.onDatesChange}
focusedInput={this.state.calendarFocused}
onFocusChange={this.onFocusChange}
showClearDates={true}
numberOfMonths={1}
isOutsideRange={()=> false}
/>
</div>
)
}
}
const mapStateToProps = (state) => {
return {
filters: state.filters
}
}
export default connect(mapStateToProps)(ExpenseListFilters)
我希望能够通过单击日历上的“ X”来清除日期,以便显示我的演示数据,或选择要过滤的特定范围。 我该如何解决?
答案 0 :(得分:0)
由于错误显示为Cannot read property 'toLowerCase' of undefined
,因此您的问题出在代码const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())
为防止出现此类问题,我希望提供一个默认值,如下所示:
export default (expenses, { text = '', sortBy, startDate, endDate }) => {
return expenses.filter((expense = {}) => {
const { description = '' } = expense
const createdAtMoment = moment(expense.createdAt)
const startDateMatch = startDate ? startDate.isSameOrBefore(createdAtMoment, 'day') : true
const endDateMatch = endDate? endDate.isSameOrAfter(createdAtMoment, 'day'): true
const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())
return startDateMatch && endDateMatch && textMatch
}).sort((a, b) => {
通过在文本,费用和描述中添加默认值,您的代码不会出错-但问题可能无法解决。
为解决该问题,我建议向每行添加控制台日志,以确保这些值与您期望的值相匹配-或使用nodejs内置的debugger
工具
答案 1 :(得分:0)
嗨,我现在解决了我的问题:我忘了在...的reducers / filters中包括... state。
public function dashboard()
{
$user = Session::get('user');
return view('user.dashboard',['data'=>$user]);
}
public function logout(Request $request) {
Auth::logout();
Session::flush();
return redirect('/login');
}
它应该是:
case 'SET_START_DATE':
return {
startDate: action.startDate
}
case 'SET_END_DATE':
return {
endDate: action.endDate
}