简体   繁体   English

如何将我在 URL 中使用的查询参数数据添加到我的响应数据中?

[英]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.

相关问题 如何将 URL 参数添加到我网站上的链接 - how can I add a URL parameter to a link on my website 如何在不重新加载页面的情况下向我的 url 添加参数? - How can I add a parameter to my url, without reload the page? 如何从我的数据文件中获取 imbdID 并将其添加到正确的 URL,以便稍后使用 DOM 将其加载到我的屏幕上? - How can i grab the imbdID, from my data file and add it to the correct URL, to the later on load it on to my screen using the DOM? 如何转换来自http.post响应json的数据,以便可以在应用程序中使用它? - How do I convert the data from a http.post response json so I can use it in my application? 我可以使用 vue-router 将下拉列表的值数据绑定到 URL 的查询参数吗? - Can I use vue-router to data-bind a dropdown's value to a URL's query parameter? 如何使用 Javascript 将查询参数 append 作为我的 URL 的查询参数? - How do I append a query parameter to my URL using Javascript? Firestore - 如何在我的 state 中添加文档 ID 及其数据? - Firestore - How can I add the Document ID and their data in my state? 如何将 JSON 数据中的行添加到我的表中? - How can I add rows from JSON data to my table? WordPress:如何将url GET参数添加到主菜单项 - Wordpress: How can I add url GET parameter to my main menu items 如何在 Javascript 上正确获取我的 url 参数? - How can i get my url parameter right on Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM