【问题标题】:API undefined issue with petfinder and ReactJSpetfinder 和 ReactJS 的 API 未定义问题
【发布时间】:2017-05-09 00:54:47
【问题描述】:

删除问题以避免回复

【问题讨论】:

  • 表示arrOfPets.petfinder 未定义
  • 谢谢你。正如您在第一个屏幕截图中看到的那样,它显然不是未定义的,这就是我要解决的问题。
  • 我看不懂截图:p

标签: javascript api reactjs


【解决方案1】:

不要将 arrOfPets 括在大括号中。试试这个:

if (this.props && this.props.petfinder) {
const arrOfPets = this.props
const allPets = arrOfPets.petfinder
}

如果你想使用解构语法,你的变量需要匹配对象的属性名。

const { petfinder } = this.props

Javascript (ES6) const with curly braces

【讨论】:

  • 我想我不明白这将如何工作,因为 petFinder 是 api 返回/承诺的第一个“节点”? this.props.petFinder 不存在我映射到道具的状态是 arrOfPets 不是 petfinder?我还是新手,所以如果我错了,只是 lmk 但它对我不起作用,它会出现错误。同样在您的 if 语句建议中,当我这样做时,我无法访问 arrOfPets 或 allPets
  • 我没有完全听从你的评论。您的代码示例测试 this.props.getPets 的存在,然后假定 this.props.arrOfPets 的存在。我认为这就是你遇到麻烦的地方。
  • arrOfPets 是我通过运行“this.props.getPets”从我的 redux 存储中获取的状态传递给道具的值。 this.props.arrOfPets 在运行完我的中间件后返回承诺数据。我唯一遇到的问题是当我尝试从商店中获取 .pets 时。在那之前的一切都运行良好,控制台也很好,所以我抓住了正确的东西,就是 .pets 把它搞砸了。不确定是我的代码还是 api。查看第一个屏幕截图,即 this.props.arrOfPets.petFinder 的 console.log。你会看到pets是那个的一个节点
  • 就像其他海报提到的,截图很难阅读。您能否将相关文字粘贴到问题中?
【解决方案2】:

深入挖掘后,似乎 api 本身存在问题。感谢您的帮助!!!

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2019-12-24
    • 2018-08-13
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多