{
"query": {
"bool" : {
"must": {
"exists": {
"field": "dossiers.keyword"
}
}
}
},
"sort": [{"change_date": "desc"}],
"collapse": {
"field": "dossiers.keyword",
"inner_hits": {
"name": "most_recent",
"size": 1,
"sort": [{"change_date": "desc"}]
}
}
}
答案 0 :(得分:1)
您的代码中有很多问题。
大概是您打算的:
const { dataEle } = this.setState;
成为
const { descriptions = [] } = this.state;
答案 1 :(得分:1)
dataEle
在第一个渲染中(以及在提取之前的所有后续渲染中)均未定义。您还无法在渲染函数中正确地对其进行重构。我认为您可能打算破坏descriptions
。
import React, { Component } from "react";
import axios from "axios";
class Abc extends Component {
constructor(props) {
super(props);
this.state = {
descriptions: [],
};
}
componentDidMount() {
axios
.get("https://jsonplaceholder.typicode.com/users")
.then(response => {
this.setState({ descriptions: response.data });
// if (response.data) {
// var rdata = response.data;
// for (var r = 0; r < rdata.length; r++) {
// if (r === 0) {
// // console.log(rdata[r]);
// // const {rdata} this.dataEle = rdata[r]
// console.log(this.dataEle.name);
// }
// }
// }
})
.catch(error => {
console.log(error);
});
}
render() {
const { descriptions } = this.state;
return (
<div>
// {descriptions.map((description, index) => (
// <p key={index}>{description.description}</p> // response data objects don't have a description property!
// ))}
{descriptions[1] && descriptions[1].name}
</div>
);
}
}
export default Abc;
此外,响应数据形状上没有description
属性,但是TBH我真的不确定您要使用for循环做什么,它会引发错误。
答案 2 :(得分:0)
请尝试:
class Abc extends Component {
constructor(props) {
super(props);
this.state = {
descriptions: [] ;
}
}
componentDidMount() {
axios
.get("https://jsonplaceholder.typicode.com/users")
.then(response => {
this.setState({ descriptions: response.data });
})
.catch(error => {
console.log(error);
});
}
//for mapping**
return (
<div>
{this.sate.descriptions.map((description, index) => (
<p key={index}>{description.description}</p>
))}
</div>
);
}
}