Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 Keyvalues pipe doesn't build for production

My app that I've created runs fine on the development server, ng serve, but when I attempt to build for production it fails with this error

ERROR in src/app/leaderboard/leaderboard.component.html(9,17): Argument of type 'object' is not assignable to parameter of type 'Map<unknown, unknown>'

Here's my leaderboard.component.ts

import { Component, OnInit } from '@angular/core';
import * as firebase from 'firebase/app';
import 'firebase/firestore';


@Component({
  selector: 'app-leaderboard',
  templateUrl: './leaderboard.component.html',
  styleUrls: ['./leaderboard.component.css']
})

export class LeaderboardComponent implements OnInit {

  constructor() { }
  db = firebase.firestore();
  cities:object;
  suburbs:object;
  unsorted() { }
  async getCities() {
    await this.db.collection("Cities").orderBy('count', "desc").get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            //console.log(doc.id, ": ", doc.data().count);
            this.cities[doc.id] = doc.data().count
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });
  }

  async getSuburbs() {
    await this.db.collection("Suburbs").orderBy('count', "desc").get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            //console.log(doc.id, ": ", doc.data().count);
            this.suburbs[doc.id] = doc.data().count
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });
  }



  ngOnInit() {
    this.getCities()
    this.getSuburbs()
  }

}

And here's the leaderboard.component.html

<div class="container">
    <div class="row">
        <h3>Cities</h3>
        <table class="table">
            <thead class="thead-dark">
                <th scope="col">City</th>
                <th scope="col">Bears found</th>
            </thead>
            <tr *ngFor="let city of cities | keyvalue: unsorted">
                <td>{{city.key}}</td>
                <td>{{city.value}}</td>
            </tr>
        </table>
    </div>
    <div class="row">
        <h3>Suburbs</h3>
        <table class="table">
            <thead class="thead-dark">
                <th scope="col">Suburb</th>
                <th scope="col">Bears found</th>
            </thead>
            <tr *ngFor="let suburb of suburbs | keyvalue: unsorted">
                <td>{{suburb.key}}</td>
                <td>{{suburb.value}}</td>
            </tr>
        </table>
    </div>
</div>

I think this is some sort of typing error from TypeScript but as I'm not much more than a beginner, I'm not quite sure what I can do to fix it.

I've left out a second error for line 22 of the html as it's the same as the error on line 9 where I use the keyvalue pipe

Thanks

like image 324
Hamish McBrearty Avatar asked Apr 17 '20 04:04

Hamish McBrearty


People also ask

What is keyvalue pipe in angular?

This keyvalue pipe converts Object or Map into an array of key value pairs. Converted array will be sorted by keys according to unicode values. And if the keys are of complex types we can pass compare function to sort the array. Go through the below angular examples to understand Angular keyvalue pipe further.

How to fix the pipe ‘keyvalue’ could not be found error?

The pipe ‘keyvalue’ could not be found error. As the keyvalue pipe introduced in Angular 6.1 release. If you try to use keyvalue pipe in older versions of Angular you will get The pipe 'keyvalue' could not be found error. You need to update @angular/core by runnin below angular cli ng command. ng update @angular/core

How to loop through objects key values&maps in angular?

KeyValue pipe released in Angular 6.1 to loop through objects,Maps and arrays.Now by passing KeyValue pipe to *ngFor we can loop through objects key values & maps. Prior to this Angular 6.1 release we cannot iterate directly through objects key values & maps using *ngFor directive.

How to sort array with complex keys in angular keyvalue pipe?

Angular KeyValue pipe uses defaultComparator function to sort the key value array. But if the key is complex,we can pass our own custom compare function to sort the array. If the object key is a complex type, the defaultComparator may not serve our purpose. We need to write our own comparator.


2 Answers

using $any() type casting is also a way to get rid of the error:

<tr *ngFor="let city of $any(cities) | keyvalue: unsorted">

like image 140
Jonathan Avatar answered Oct 17 '22 01:10

Jonathan


Having the "strictTemplates" flag enabled implies that "fullTemplateTypeCheck" is also enabled, so the latter can not be explicitly disabled.

One of the following actions is required:

  1. Remove the "fullTemplateTypeCheck" option.
  2. Remove "strictTemplates" or set it to 'false'.
like image 30
Roshan Khandelwal Avatar answered Oct 17 '22 00:10

Roshan Khandelwal