0
onMounted(() => {
  productService.value
    .getProducts()
    .then((data) => (products.value = data));

  console.log((products))
});

When I print products with console.log, here what I have.

capture of the console

I see that the data I want are in RawValue but I don't know how to access them.
I tried Object.values(products) or just console.log(products._rawValue) or console.log(products.rawValue) it print undefined.

Do you know what function call ?

Thanks

4
  • You need to define variables in the data property and fill them with incoming data. Commented Jul 5, 2021 at 13:13
  • How I fill them with the incoming data ? Commented Jul 5, 2021 at 13:34
  • Try console logging 'data' to verify it's structure. You may actually need to set 'products' (or 'products.value') to a property of data i.e. something like products.value = data.products. Commented Jul 5, 2021 at 13:37
  • Hi @KateP, I just wanted to make sure, is this data coming from firebase? P.S. It will really help us if you tell us what package are you using to receive that data. Commented Jul 5, 2021 at 15:04

2 Answers 2

1

There are 2 issues

#1 - you're using console.log(products) which shows you the reactive object, what you need instead is console.log(products.value) which will only show the value, which should match the content of data.produtcs

#2 - you might find that 👆 now shows an empty result. The reason that's happening is that you're calling the console log after the async function, but before it finishes, so you're calling it before it has a chance to update. To fix that, you can log as part of the async function

onMounted(() => {
  productService.value
    .getProducts()
    .then((data) => {
      products.value = data;
      console.log(products.value);
    })
});

If you're using the products inside a template, you don't need to worry about what's before or after the async function since it will re-render the component on change.

Also, you probably don't need to define productService as a ref, the class is likely not something that needs to be reactive, so you can just do simple assignment and then skip the .value to call getProducts

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much it worked ! Indeed I was trying products.value but it was showing an empty result, now placed where you said it's fine
0

with axios what I do is take out the data with response.data you could try

                    onMounted(() => { 
                    productService.value.getProducts().then((response) => (
                        products = response.data
                        )); 
                        console.log(products.length); 
                });

2 Comments

Thank you very much for your answer. Unfortunatly it doesn't work because products is not directly the array but in products there is the array in rawValue
try with onMounted(() => { productService.value .getProducts() .then((response) => (products= response.data)); console.log(products); });

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.