【问题标题】:Uncaught TypeError: Cannot read properties of null - React.js未捕获的类型错误:无法读取 null 的属性 - React.js
【发布时间】:2022-07-06 00:59:23
【问题描述】:
const deletePerson = (persons) => {
axios
  .delete(`http://localhost:3001/persons/${persons.name}`)
  .then(console.log(`done`))
  .then(response => response.data)
  .then(setPersons(Object.values(persons).filter(q => q.name !== false)))}

我是 React.js 的新手。 我有一个问题,我已经尝试解决了几天。 我为删除请求添加了按钮,并且以不同的方式返回相同类型的错误: 未捕获的 TypeError:无法读取 null 的属性(正在读取“名称”)。

error message

【问题讨论】:

  • 你能安慰和分享persons对象吗?
  • { "persons": [ { "name": "Arto Hellas", "number": "040-123456", "id": 1 }, { "name": "Ada Lovelace" ,“数字”:“39-44-5323523”,“id”:2 },{“名称”:“丹·阿布拉莫夫”,“数字”:“12-43-234345”,“id”:3 },{ “姓名”:“玛丽波彭迪克”,“号码”:“39-23-6423122”,“身份证”:4 } ] }
  • 请将代码添加为文本而不是图像
  • 您提供了一个 JSON 数组。您无法从 json 数组访问 person.name。您必须过滤并发送人名。
  • 你能解释一下用例吗?

标签: javascript reactjs axios


【解决方案1】:

看起来您使用的是基于整个对象的数组值,而不是对象内的 person 值。从外观上看,您正试图找到一个值为 false 的名称,该名称似乎与您在那里的名称不匹配。

这样的东西可能就是你要找的东西

.then(setPersons(Object.values(personsList).filter(q => q.name !== persons.name)))}

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2022-01-19
    • 2021-12-01
    • 2022-07-02
    • 2022-06-14
    • 2022-10-23
    • 2023-03-05
    相关资源
    最近更新 更多