NuxtN
Nuxt13mo ago
KWAC

fetching data from database

is this correct way to get data? for some reason when i enter page console log doesn't print anything

const { data, error } = useAsyncData('getArticleData', async () => {
  try {
    const response = await fetch('/api/blog/getArticleData', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ url: inputURL }), // Pass inputURL in the request body
    });

    if (!response.ok) {
      throw new Error('Failed to fetch data');
    }

    const jsonData = await response.json();
    console.log(jsonData);

    return jsonData; //
  } catch (err) {
    console.error('Error fetching data:', err);
    throw err; //
  }
});
Was this page helpful?