簡體   English   中英

我的過濾器提交 function 沒有按預期工作。 未捕獲的類型錯誤:country.match 不是 function

[英]My filter submission function isn't working as intended. Uncaught TypeError: country.match is not a function

我的代碼不適用於 country.name.match function 無法按預期工作。

錯誤代碼如下:App.js:27 Uncaught TypeError: country.name.match is not a function

REST API 正在使用: https://restcountries.com/#api-endpoints-v3-all

如果需要,代碼的 Rest: https://github.com/jnatividad-design/FSO/tree/main/part2/countries

 const handleFilterSubmit = (event) => { console.log(event.target.value) setNewFilter(event.target.value) const regex = new RegExp ( newFilter, 'i'); const filteredCountries = () => countries.filter(country => country.name.match(regex)); setCountries(filteredCountries) }

我發現出了什么問題。 REST API 稍微改變了數據庫的結構,增加了國家及其名稱的特異性。 原來是 country.name ,它必須是 country.name.common ,因為名稱是另一個 object ,具有通用名稱、官方名稱等屬性。

 const handleFilterSubmit = (event) => { console.log(event.target.value) setNewFilter(event.target.value) const regex = new RegExp ( newFilter, 'i'); const filteredCountries = () => countries.filter(country => country.name.common.match(regex)); setCountries(filteredCountries) }

如果不看更多代碼就很難說(例如,知道“國家”的樣子會有所幫助),但我的第一個猜測可能是country.name不是字符串。 您是否驗證過您嘗試調用match()的值是一個字符串?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM