Need help to import News API with axios on React

I am try to make an api call to News API, but I get an error message “can’t resolve”, I XXX out my API key, so you its not posted here. Code below:

import axios from 'axios';

export const getNews = async()=>{ const result = await axios.get ('https://newsapi.org/v2/everything?q=bitcoin&from=2019-11-15&sortBy=publishedAt&apiKey=API_KEYXXX') .then(data=>data) return result }

I’m not familiar with using axios … but this is from the NPM page:

// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

I’m not sure your project or requirements but I recently did a simple app with Google API (sheets) to read data. I used Node Fetch to save the data to State, once componentDidMount.

… edit (i could be way off here since I still need more practice with Promises) but since this is being exported, should the expecting function be async as-well and expecting Resolve/Reject (not return?)

1 Like

I tried same host, path and query params using python requests and my own API key for this api. It is working fine.
The axios cannot resolve error could be setup issue.

not help in python… help in react.

Please try :slightly_smiling_face:

import axios from "axios";

export default async () => {
  const tokenURL =
    "http://newsapi.org/V2/everything?q=bitcoin&from=2019-11-30&sortBy=published At&apikey=xxx";

  try {
    const response1 = await axios.get(tokenURL);
    return response1;
  } catch {
    return "  ";
  }
};
1 Like