因此,我正在尝试从数组中删除一些元素。我有2个过滤器需要调用。一种是使用搜索框过滤数据,另一种是根据条件过滤。但我收到此错误:
TypeError:TypeError:无法读取未定义的属性“ filter”
这是组件:
const PassengerCardBasedOnRoute = ({
searchParam,
passengerCardId,
isAddToMyPassengersSuccess,
unassignedDropOffPassengers,
}) => {
// This is the filter that work based on a search parameter
const filteredData = filterByParam =>
filterByParam.filter(obj =>
Object.keys(obj).some(key =>
String(obj[key])
.toLowerCase()
.includes(searchParam.toLowerCase()),
),
);
// Here is where I need to remove from the array the condition met
const filterByPassengerCardId = passengersData => {
remove(passengersData, info => {
return info.id === passengerCardId;
});
};
const componentToRenderBasedOnParams = info => (
<View key={info.id}>
{isAddToMyPassengersSuccess && info.id === passengerCardId && (
<PassengersAdded name={info.name} id={info.id} />
)}
<PassengersInfo
id={info.id}
name={info.name}
searchParam={searchParam}
/>
</View>
);
const showFeedbackIfNoLength = data => {
if (!passengerCardId && size(filteredData(data))) {
filteredData(data).map(info => componentToRenderBasedOnParams(info));
} else if (passengerCardId) {
// HERE IS WHERE THE APP THROWS THE ERROR
filteredData(filterByPassengerCardId(data)).map(info =>
componentToRenderBasedOnParams(info),
);
} else {
return <EmptyState>No records found</EmptyState>;
}
return data;
};
return (
<>
<OptionsModal>{<AllPassengersOptionsModal />}</OptionsModal>
<View>
{!navigationStore.index && unassignedDropOffPassengers
? showFeedbackIfNoLength(unassignedDropOffPassengers)
: null}
</View>
</>
);
};
编辑
出现错误:
const filteredData = filterByParam =>
// Here comes the error
filterByParam.filter(obj =>
Object.keys(obj).some(key =>
String(obj[key])
.toLowerCase()
.includes(searchParam.toLowerCase()),
),
);
满足此条件时:
// HERE IS WHERE THE APP THROWS THE ERROR
filteredData(filterByPassengerCardId(data)).map(info =>
componentToRenderBasedOnParams(info),
);
满足此其他条件后,一切正常:
if (!passengerCardId && size(filteredData(data))) {
filteredData(data).map(info => componentToRenderBasedOnParams(info));
}
要实现自己的目标,我缺少什么?
答案 0 :(得分:2)
在表达式filteredData(filterByPassengerCardId(data))
中,filteredData
似乎期望Array
;但是,filterByPassengerCardId(data)
返回未定义。
将filterByPassengerCardId
的返回值更改为Array
,该错误不应再引发。
编辑:看到您的评论是您使用的是Lodash的remove
method,实际上它会返回Array
。
在这种情况下,只需编辑filterByPassengerCardId
即可返回调用remove
的结果:
const filterByPassengerCardId = passengersData => {
return remove(passengersData, info => {
return info.id === passengerCardId;
});
};
编辑:为简洁起见,或者更好:
const filterByPassengerCardId = passengersData => (
remove(passengersData, ({id}) => id === passengerCardId)
);
答案 1 :(得分:1)
问题可能来自这里:
$server_ip= $_POST['server'];
$port = $_POST['port'];
$server_ip = gethostbyname($server_ip);
$status = array();
if (empty($_POST["server"]) || empty($_POST['port']) || !filter_var($server_ip, FILTER_VALIDATE_IP) )
{
echo "some html code";
}
elseif (!(is_numeric($port)))
{
echo "some other html code";
}
else
{
if($pf = @fsockopen($_POST['server'], $port, $err, $err_string, 1)) {
$status = true;
fclose($pf);
} else {
$status = false;
}
Output code here..
}
也许它没有按照您期望的方式工作,至少它没有返回任何东西。您可能会期望从此函数返回数组。