0

I have a nuxt 3 app that fetches data from the API. I would like to use that data to populate the input fields in the template but I keep getting an error.

Here is a snippet of my code

<script setup>

const config = useRuntimeConfig();

const route = useRoute();

const router = useRouter();

const { data: pkg } = useFetch(
  () => '/api/id/1/'
);

const request = ref({
  field: pkg.value.field_value,
});

When I console.log(pkg.value.field_value) I get the value printed on the browser developer tools console tab but on hard refresh, I get the error Cannot read properties of null (reading 'field_value')

The reason why I need to dynamically set the value of field is so that I am able to update it.

Anyone encountered that problem before and how did you address it

1 Answer 1

2

Add await to the useFetch function because at the first rendering the pkg is not available :

<script setup>

const config = useRuntimeConfig();

const route = useRoute();

const router = useRouter();

const { data: pkg } = await useFetch(
  () => '/api/id/1/'
);

const request = ref({
  field: pkg.value.field_value,
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.