[英]How can I add my query parameter data -which I use in URL- to my response data?
I'm sending a request to get organization data's from API firstly, then ı getting another specific data's -which is "plants" - from another API with sending as a parameter organizationEIC property of my organization data which ı get in the first request.我首先发送从 API 获取组织数据的请求,然后我从另一个 API 获取另一个特定数据 - 这是“植物” - 作为参数发送我的组织数据的组织EIC 属性,这是我在第一个请求中获得的。 I am doing this to get for each plants of each organizations.我这样做是为了获得每个组织的每个工厂。 What ı want to do is, I need to add organizationEIC property which ı use in the URL query to the response data's which are came as a result of my request.我想要做的是,我需要将我在 URL 查询中使用的organizationEIC属性添加到响应数据中,这是我的请求的结果。 Let me make this clear with one example ;让我举一个例子来说明这一点;
I am sending a request like ** https://seffaflik.epias.com.tr/transparency/service/production/dpp-injection-unit-name?organizationEIC=40X000000000104H and as you can see it responses to me with 5 different plant data .我正在发送类似** https://seffaflik.epias.com.tr/transparency/service/production/dpp-injection-unit-name?organizationEIC=40X000000000104H 之类的请求,您可以看到它对我的不同响应数据。 I need to add the organizationEIC property -which ı use for get this 5- to this 5 response data and save like this to database to record which organization they are affiliated with.我需要将 organizationEIC 属性(我用于获取此 5)添加到这 5 个响应数据中,并像这样保存到数据库中以记录他们所属的组织。 In another request with a different organizationEIC code, maybe it gets 2 plant data , so ı need to add my organizationEIC to this 2 plant data.在另一个具有不同组织 EIC 代码的请求中,它可能会获得2 个工厂数据,所以我需要将我的组织 EIC 添加到这 2 个工厂数据中。 So how can ı get this data from url to add my result datas in foreach loop?那么我如何从 url 获取这些数据以将我的结果数据添加到 foreach 循环中?
I hope ı explained my problem clear and easy to understand.我希望我能清楚易懂地解释我的问题。 So here is my codes ;所以这是我的代码;
var datas = []
var result = []
var plantList = []
class App extends Component {
state = {
companies: [],
plants: []
}
componentDidMount() {
fetch('https://seffaflik.epias.com.tr/transparency/service/production/dpp-organization', {
method: "GET",
headers: {
"Content-Type": "application/json",
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response =>
response.json())
.then(async resultJson => {
this.setState({
companies: resultJson.body.organizations,
})
await this.getPlants(resultJson.body.organizations) //first get org. datas and send as param.
});
}
getPlants = async (items) => {
const data = await Promise.all(items.map((plant) => { //getting plants for each organization
return fetch(`https://seffaflik.epias.com.tr/transparency/service/production/dpp-injection-unit-name?organizationEIC=${plant.organizationETSOCode}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
'X-Requested-With': 'XMLHttpRequest'
}
}).then(response =>response.json()) //need some logic for my solution here probably :(
}))
data.forEach(element => {
datas.push(element.body.injectionUnitNames)
});
Array.prototype.push.apply(plantList, this.configure(datas))
this.setState({
plants: plantList
})
}
configure = (units) => {
units.forEach((unit) => {
unit.forEach((item) => {
result.push(item)
})
})
return result
}
I am open for any tips and tricks, thx in advance for your helps and advices:)我愿意接受任何提示和技巧,提前感谢您的帮助和建议:)
Add another then()
to your fetch()
and map the data with the new property将另一个then()
添加到您的fetch()
和 map 具有新属性的数据
Something like:就像是:
const data = await Promise.all(items.map((plant) => { //getting plants for each organization
const {organizationETSOCode} =plant;
return fetch(`https://seffaflik.epias.com.tr/transparency/service/production/dpp-injection-unit-name?organizationEIC=${organizationETSOCode}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
'X-Requested-With': 'XMLHttpRequest'
}
}).then(response =>response.json())
.then(data=> data.map(o => ({...o, organizationETSOCode})))
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.