0

I am using axios to get an element value that I need to handle it as a string.

const aa = await axios
  .get(`document.querySelector('h2').innerText;`)
  .then((r) => r.data.result.value);

console.log(aa);

then, I need to check for example if the element aa.startsWith("somestring"), but is not accepted, even aa is logged in the console with the expected value.

In general, how could I convert the const result from axios into a string?

DecPK
  • 24,537
  • 6
  • 26
  • 42
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Robin Zigmond Nov 06 '21 at 15:31
  • @RobinZigmond no, so I am getting the expected value so the call is awaited. My question is more about how to cast the response into a string to do a comparison – Jaume Nov 06 '21 at 15:33
  • Oh I'd missed that you were already using `await` here. In that case `aa` will already be the string that you want, so what's the problem? – Robin Zigmond Nov 06 '21 at 15:36
  • oh except your axios call doesn't make sense - you're not making an HTTP request, you're just using `document.querySelector` to inspect something from the DOM of the current page. You don't need axios, or Promises, for that - you can just do `const aa = document.querySelector('h2').innerText`! – Robin Zigmond Nov 06 '21 at 15:37
  • I don't understand. What you're passing to axios now isn't a URL. Do you intend to use the content of the DOM element as the request's URL? – Dave Newton Nov 06 '21 at 16:05

0 Answers0