reactjs - React无法读取componentDidMount中未定义的属性'then'
问题描述
我在 componentDidMount 中使用两个 API 进行获取。第二次获取需要第一次获取的 ID 才能获取。
第一次提取效果很好,但是当我想通过它进行第二次提取时,我收到一个错误:无法读取未定义的属性'then'
Orders json 的一部分如下:
{
"orders": [
{
"deadline": 1563046159,
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"id": "83f007d6",
"name": "Work order 83f007d6",
"workerId": 1
},
{
"deadline": 1562752687,
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"id": "cb23c526",
"name": "Work order cb23c526",
"workerId": 1
},
]
}
工人json如下:
{
"worker": {
"companyName": "Topiczoom",
"email": "fstorie0@topiczoom.com",
"id": 0,
"image": "http://dummyimage.com/250x250.jpg/ccccff/000000",
"name": "Frans Storie"
}
}
class App extends Component {
constructor(props) {
super(props);
this.state={
ordersData: [],
workersData:[]
}
}
componentDidMount() {
fetch("https://www.hatchways.io/api/assessment/work_orders")
.then(response => response.json())
.then(data => {
this.setState({
ordersData: data.orders
});
console.log(this.state.ordersData)
.then(this.state.ordersData.map(order =>{
console.log(this.state.ordersData)
fetch(`https://www.hatchways.io/api/assessment/workers/${order.workerId}`)
.then(result => result.json())
.then( data2 =>{
this.setState ({
workersData : data2.worker
})
})
}))
console.log(this.state)
})
.catch(err => {
console.log(err)
});
}
无法读取未定义的属性“then”
解决方案
你不能像你一样在没有inside 的.then
情况下直接使用。fetch
fetch
您需要根据第一个 API 响应中设置的状态调用另一个 API callback
,setState
fetch("https://www.hatchways.io/api/assessment/work_orders")
.then(response => response.json())
.then(data => {
this.setState({
ordersData: data.orders
}, () => { //callback of setState, now here you can access state
console.log(this.state.ordersData)
this.state.ordersData.map(order => {
console.log(this.state.ordersData)
fetch(`https://www.hatchways.io/api/assessment/workers/${order.workerId}`)
.then(result => result.json())
.then(data2 => {
this.setState({
workersData: data2.worker
}, () => console.log(this.state))
})
})
});
})
.catch(err => {
console.log(err)
});
更新
要存储所有记录,
() => { //callback of setState, now here you can access state
console.log(this.state.ordersData)
let workersData = [];
this.state.ordersData.map(order => {
console.log(this.state.ordersData)
fetch(`https://www.hatchways.io/api/assessment/workers/${order.workerId}`)
.then(result => result.json())
.then(data2 => {
workersData.push(data2.worker)
})
})
this.setState({
workersData
}, () => console.log(this.state))
});
推荐阅读
- ios - 基于本地通知刷新 SwiftUI 视图
- flutter - 无法在 Flutter 中创建命名构造函数
- python - 如何使用 aiohttp 处理非 200 状态?
- javascript - 调度程序syncfusion vue中的更改编辑器模式在渲染中不起作用
- python - Py auto gui返回屏幕外的坐标
- vue.js - vue multiselect:如果为空,则删除组标签
- python - Python group by 正在创建列表列表而不是单个列表
- typescript - 打字稿中带有“this”的调用签名
- swiftui - 不能在同一个View中多次使用基于UIView的UIViewRepresentable
- reactjs - 将 Fluent UI Nav 中的 Chevron 移动到右侧