无法使用默认值读取未定义的 属性
cannot read property of undefined using default value
我有一个模型可以获取一些数据和过滤器。当应用程序启动时,我希望所有过滤器都等于 true
。我已经设置了默认值,但我仍然收到以下错误。
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
TypeError: Cannot read properties of undefined (reading 'placeFilter')
at model (model.js:1)
at init (index.js:15)
您的参数可以被命名以访问默认值
const model = (data, predictions, filter = { placeFilter: true, startDateFilter: true, endDateFilter: true }) => {
const getData = () => data.filter(filter.placeFilter).filter(filter.startDateFilter).filter(filter.endDateFilter)
}
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
你不能通过像 model([], [])
这样的传递两个参数来调用上面的 model
函数,因为 JavaScript 会尝试解构第三个参数,即 undefined
.
您可以这样做:
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
console.log(placeFilter, startDateFilter, endDateFilter)
}
model([], [])
model([], [], { placeFilter: false })
BTW 假设 data
是一个数组并且您正在尝试使用 Array.prototype.filter
,filter
应该使用 returns 一个布尔值的回调函数来调用,但是您'直接将布尔值传递给所有过滤器。
我有一个模型可以获取一些数据和过滤器。当应用程序启动时,我希望所有过滤器都等于 true
。我已经设置了默认值,但我仍然收到以下错误。
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
TypeError: Cannot read properties of undefined (reading 'placeFilter')
at model (model.js:1)
at init (index.js:15)
您的参数可以被命名以访问默认值
const model = (data, predictions, filter = { placeFilter: true, startDateFilter: true, endDateFilter: true }) => {
const getData = () => data.filter(filter.placeFilter).filter(filter.startDateFilter).filter(filter.endDateFilter)
}
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
你不能通过像 model([], [])
这样的传递两个参数来调用上面的 model
函数,因为 JavaScript 会尝试解构第三个参数,即 undefined
.
您可以这样做:
const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
console.log(placeFilter, startDateFilter, endDateFilter)
}
model([], [])
model([], [], { placeFilter: false })
BTW 假设 data
是一个数组并且您正在尝试使用 Array.prototype.filter
,filter
应该使用 returns 一个布尔值的回调函数来调用,但是您'直接将布尔值传递给所有过滤器。