I'm trying to calculate total bill (sum of all product*usage) from an object of customerProduct data and display the bill amount. I'm calling a computed method to perform this. The customerProduct data is fetched from a get
api call in the created()
method.
Issue: In the initial render, console shows the following error: [Vue warn]: Error in render: "TypeError: Cannot read property 'PRICE' of undefined"
. Is this because the computation is taking some time and meanwhile when the template html code renders, the customerProductData is not fetched properly?
Also, can using watch
property help here?
Computed method to calculate total bill:
computed:{
billAmount(){
var billAmountWithoutDiscount = 0;
if(typeof(this.customerProductData[0].PRICE) == undefined){
return 0.0
}
else{
for(let i=0;i<this.customerProductData.length;i++){
billAmountWithoutDiscount += this.customerProductData[i].PRICE * this.customerProductData[i].USAGE;
}
return Number((100.0 - this.customerMetaData.discount)*billAmountWithoutDiscount/100).toLocaleString();
}
}
}
GET api call:
methods:{
async init(){
const response = await axios.get("/subscribe/getPresalesPricingMetaData/"+this.customerName)
this.customerProductData = response.data;
// console.log(response.data)
this.getCustomerMetaData();
},
}
customerProduct object:
customerProductData:[
0: {
'PRICE': 10,
'USAGE': 2000
},
1: {
'PRICE': 30,
'USAGE': 230
},
2: {
'PRICE': 50,
'USAGE': 200
},
3: {
'PRICE': 30,
'USAGE': 1000
},
]
The discount value:
customerMetaData:{
'discount': 2.2
}