Search code examples
angulartypescriptionic-frameworkcapacitor

Property 'values' does not exist on type 'unknown'. ionic/capacitor


so i have been following this tutorial https://devdactic.com/sqlite-ionic-app-with-capacitor/ but I got an error in home.page.ts for this.products= res.values; it said Property 'values' does not exist on type 'unknown'. not sure what does it mean and how to solve it, i am totally beginner in ionic and hope anyone can help me to explain and help me with this.

enter image description here

enter image description here


Solution

  • It's an error from typescript. By default in angular tsconfig.json it has a configuration noImplicityAny. That means, you have to specify types, otherwise typescript will yell at you

    Quick Fix (not-recommendable)

    this.databaseService.getProductList().subscribe((res: any) => {
       this.products = res.values;
    });
    

    Recommended Fix

    Service

    interface Product {
       ...
    }
    
    interface ProductResponse {
       values: Product[];
    }
    
    getProductList() {
       return this.http.get<ProductResponse>('https://url/to/api');
    }
    

    // HomePage

    products: Product[] = [];
    
    this.databaseService.getProductList().subscribe((res) => {
       this.products = res.values;
    });