Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Need for product_price to be a number

Jul 17 2020 6:46 AM
When I get the product_price it returns "Unsupported value type" as a result of the function below but I need for it to be a number. I tried this.product_price = parseFloat(this.product_price); but that returns the error: Argument of type 'number' is not assignable to parameter of type 'string'. How do I cast product_price as a number?
In my HTML:
  1. <div>{{ totalPrice }}</div>  
  2. <ul *ngFor="let product of products">  
  3. <li>  
  4. <div>{{product.price }}</div>  
  5. <button type="submit" class="btnAddAction" (click)="onSubmit( [product.price] )">Add to Cart</button>  
  6. </li>  
  7. </ul>  
The HTML is getting the products from the database which was created with this JSON:
  1. {  
  2. "products": [  
  3. {  
  4. "price": 20.50,  
  5. },  
  6. {  
  7. "price": 20.50,  
  8. },  
  9. {  
  10. "price": 12.75,  
  11. }  
  12. ]  
  13. }  
In my controller typescript file:
  1. get totalPrice() {  
  2. if (typeof this.product_price === "string") {  
  3. return "string";  
  4. }  
  5. if (typeof this.product_price === "number") {  
  6. return "number";  
  7. else {  
  8. throw Error("Unsupported value type")  
  9. }  
  10. }  
  11. product_price: number;  
  12.   
  13. onSubmit(product_price){  
  14. product_price = parseFloat(product_price);  
  15. const data = {  
  16. product_price  
  17. };  
  18. this.items.push(data);  
  19. localStorage.setItem('items', JSON.stringify(this.items));  
  20. }  


Answers (1)