reactjs - 映射 axios 响应,然后将其用于另一个响应
问题描述
我想映射一组图像,但因为 Directus 将图像或文件标识为集合项目中的 id。
现在我需要从第一个请求中的响应中映射 id 数组,然后使用该 id 在另一个端点发出另一个 get 请求,这就是文件。
我使用的数据库是DirectusCms,前端是react.js
class ComponentToPrint extends React.Component {
constructor(props) {
super(props);
this.handleLoad = this.handleLoad.bind(this);
this.state = {
nama: [],
kk:[],
ijazah:[]
};
}
componentDidMount() {
window.addEventListener('load', this.handleLoad);
}
componentWillUnmount() {
window.removeEventListener('load', this.handleLoad)
}
handleLoad() {
let nama_1;
let id_kk;
const url = `https://sensor/public/gemaclc/items/pendaftar?access_token=sensor`
const url2 = `https://sensor/public/gemaclc/files/${id_kk}?access_token=sensor`
axios(url, {
method: 'get',
headers:{ 'Content-Type': 'application/json'} })
.then(res => {
const nama = res.data.data;
const nama = res.data.data;
console.log( nama)
this.setState( {nama} );
})
.catch(error => console.error('Error:', error))
// .then(res => {console.log('Success:', nama)})
// this.setState( nama );
// console.log(nama)
axios(url2,{
method:`get`,
headers:{ 'Content-Type': 'application/json'} })
.then(res => {
id_kk = res.data.data;
console.log( id_kk)
// this.setState( {id_kk} );
})
}
render() {
return (
<>
{this.state.nama.map(node =>
<div key={node.id}>
<h2>Nama :{node.nama}</h2>
<h2>Tanggal lahir : {node.tanggal_lahir}</h2>
<h2>Tempat lahir : {node.tempat_lahir}</h2>
<h2>Email : {node.email}</h2>
<h2>Email : {node.telepon}</h2>
<h2>kk : {node.kartu_keluarga}</h2>
</div>
)}
</>
);
}
}
解决方案
当从items
Directus 中的端点请求时,可以提供一个名为 的查询参数fields
,这样您就可以从关系中获取嵌套信息,包括哈希等,请参阅:https ://docs.directus.io/api/items.html#fields-可选的
如果你有private
哈希,你可以使用assets
端点来获取实际的图像,见:https ://docs.directus.io/api/assets.html#get-an-asset
PS:你没有提到你的问题是什么,所以我根据我对该主题的整体了解猜测它 - 下次还要提到你的确切问题,你尝试了什么,什么没用等
推荐阅读
- spring-boot - 在 Spring 中消费 Kafka 消息
- git - 如何在 VisualStudio 2012 中连接到 git repo?
- css - 导航栏折叠后的菜单边距 Bootstrap 4
- javascript - 有没有办法使用纯 JavaScript 而没有 HTML/CSS 来设置 p5js 滑块的样式?
- java - 我想刷新我的 Java 框架!(制作时钟程序)
- html - 在 Excel VBA 中使用 WinHTTPRequest 访问网站的表格
- clang - -FreeBSD 10.x 上 cc 的管道标志
- javascript - javascript视频标签控制缓冲
- c# - 管理员和用户登录 C#
- batch-file - 使用批处理文件删除特定的行号