【发布时间】:2018-11-06 06:24:56
【问题描述】:
我已经使用 Rails 设置了一个 API,其中 http://localhost:3001/api/words 端点公开了以下数据:
[{"id":1,"term":"Reach","definition":"Reach is the number of people who had any content from your Page or about your Page enter their screen.","example":"","author":"Loomly","credit":"https://www.loomly.com/","created_at":"2018-11-02T03:21:20.718Z","updated_at":"2018-11-02T03:21:20.718Z"},{"id":2,"term":"Emoji","definition":"A small digital image or icon used to express an idea, emotion, etc., in electronic communication","example":"","author":"Loomly","credit":"https://www.loomly.com/","created_at":"2018-11-02T03:23:50.595Z","updated_at":"2018-11-02T03:23:50.595Z"}]
我现在尝试在使用 Create React App 构建的 React.js 前端应用程序中简单地显示这些数据(理想情况下作为无序列表),这是我的 App.js 文件的内容:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor () {
super()
this.state = {}
this.getWords = this.getWords.bind(this)
this.getWord = this.getWord.bind(this)
}
componentDidMount () {
this.getWords()
}
fetch (endpoint) {
return window.fetch(endpoint)
.then(response => response.json())
.catch(error => console.log(error))
}
getWords () {
this.fetch('/api/words')
.then(words => {
if (words.length) {
this.setState({words: words})
this.getWord(words[0].id)
} else {
this.setState({words: []})
}
})
}
getWord (id) {
this.fetch(`/api/words/${id}`)
.then(word => this.setState({word: word}))
}
render () {
let {words, word} = this.state
return (
<div>
{Object.keys(words).map((key) => {
return (
<div key={word.id}>
<p>{word.term}</p>;
</div>
)
})}
</div>
)
}
}
export default App;
我认为问题出在代码的以下区域:
render () {
let {words, word} = this.state
return (
<div>
{Object.keys(words).map((key) => {
return (
<div key={word.id}>
<p>{word.term}</p>;
</div>
)
})}
</div>
)
}
我尝试按照this tutorial 和that other tutorial 中说明的步骤进行操作,同时保持页面布局尽可能简单(semantic-ui-css 没有花里胡哨),无论如何我尝试,我不断进入以下错误:
TypeError: Cannot convert undefined or null to objectUnexpected token, expected “,”Failed to compile: 'word' is not defined no-undef
this article 中解释的解决方案让我找到了我现在拥有的代码,但我在构建 React 应用程序的方式上缺少一些东西:你能指出我正确的方向吗?
【问题讨论】:
-
开头
this.state是空对象,所以render中的words和word是undefined。同样来自给定信息words似乎是一个数组,所以键代表索引。/api/words/${id}返回的数据是否与您在words[0]中的数据不同?
标签: javascript ruby-on-rails json reactjs rails-api