0

Is there a way to use const { data } = await axios.get(insertthinghere); multiple times?

Trying to use AXIOS to get stuff from APIs, using the code above works once however when trying to use it again, it throws an error.

Cannot redeclare block-scoped variable 'data'

Trying to change data to anything else also doesn't work.

Example of what I'm trying to do with placeholder names.

let api1 = "INSERTAPILINKHERE";

    if (args.length) {
      api1 += `/++${args[0]}`;
    }

    const { data } = await axios.get(api1);

    var variable1 = JSON.stringify(data);

    variable1 = variable1.replace(/\D/g, "");

    let api2 = "ANOTHERDIFFERENTAPILINK";

    if (args.length) {
      api2 += `/++${args[0]}`;
    }

    const { data2 } = await axios.get(api2);

    const parsedJSON = JSON.parse(data2);

    var valueFromJSON = parsedJSON.somevalue;
  • 2
    You don't need to _define_ the variable the second time you do it: https://stackoverflow.com/q/59416204/3001761 – jonrsharpe Apr 13 '22 at 19:32
  • Do this the second time: `({ data } = await axios.get(otherthinghere))` – ShamPooSham Apr 13 '22 at 19:49
  • @ShamPooSham Tried that, it worked but now I've got another problem on my hands where it seems to think the data from the second API isn't JSON, and throws `SyntaxError: Unexpected token o in JSON at position 1` at me. – Throwaway644584 Apr 13 '22 at 20:06
  • Put it in a function and call it multiple times? Or put it in a loop? – Bergi Apr 13 '22 at 20:12
  • You might want to use multiple block scopes to avoid re-declaring the variable. Or just use a different variable name, like you already did, but then you need to change the destructuring syntax to still use the `.data` property. See [here](https://stackoverflow.com/questions/34904523/es6-es2015-object-destructuring-and-changing-target-variable) for how to do that, or just don't use destructuring and write an ordinary `const data2 = (await …).data;` – Bergi Apr 13 '22 at 20:15
  • @Throwaway644584 you probably wanted to use JSON.parse or other way around – Konrad Apr 13 '22 at 20:18
  • Figured out why my JSON won't parse correctly. The API is giving me this back. (Variables and Values changed.) `{"data":[{"variable":1234,"string":"Hello World.","string2":"Lorem Ipsum."}]}` Not sure what to do with this to be honest, tried doing something similar to this but it didn't work. `parsedJSON.data.string2` – Throwaway644584 Apr 13 '22 at 20:38
  • @Throwaway644584 parsesJSON.data is an array, so you'll have to loop over that or take the first element with `[0]` if you know if only contains one element – ShamPooSham Apr 14 '22 at 10:27

1 Answers1

1

The syntax of const { data } = await axios.get(…) is called destructing and is equivalent to: const data = (await axios.get(…)).data. It a shortcut take a specific field from an object. So writing this line twice is the same as defining two consts with the same name.

The solutions are:

  1. Rename the variable const { data: newName } = …
  2. Avoid using destructing and use the long way as I wrote above