I am fetching data from the server and it's nested object. When I am trying to access nested object value it prints in console but not it the template.
For Example: consider below object
let orders = reactive({
firstName: "Rohan",
lastName: "Sharma",
payment_details: {
order_id: 1222,
address: {
city: "Dwarka",
state: "Delhi",
},
},
});
- When I print orders.value.firstName it prints the value in template
- When I print orders.value.payment_details it prints the whole object in template
- When I print orders.value.payment_details.order_id it's giving me error ( Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'payment_details'))
My Code
<script>
import { reactive, ref, onMounted } from 'vue';
import ApiService from "@/core/services/ApiService";
import { useRoute } from "vue-router";
export default {
name: "order-detail",
setup() {
const orderId = ref();
const orderDetails = reactive({});
const route = useRoute()
function getOrderDetails() {
ApiService.query("orders/"+orderId.value)
.then(({ data }) => {
orderDetails.value = data.data;
console.log(orderDetails.value.payment_details.order_id);//This prints in console but while print it in template it gives error ( Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'payment_details'))
})
.catch(({ err }) => {
console.log(err);
});
}
onMounted(() => {
orderId.value = route.params.id;
getOrderDetails();
});
return {
orderDetails,
};
},
};
</script>