What is the correct way to pass a C# dictionary into a TypeScript Map?
[HttpGet("reportsUsage")]
public IActionResult GetReportsUsage()
{
//var reportsUsage = _statService.GetReportsUsage();
IDictionary<int, int> test = new Dictionary<int, int>();
test.Add(1, 20);
test.Add(2, 30);
test.Add(3, 40);
test.Add(4, 50);
test.Add(5, 70);
test.Add(6, 60);
test.Add(7, 90);
test.Add(8, 30);
return Ok(test);
//return Ok(reportsUsage );
}
In Angular:
getReportsUsage() {
return this.http.get<Map<number, number>>(`${environment.apiUrl}/stats/reportsUsage`, {
headers: new HttpHeaders({
'Content-Type': 'text/plain',
'Accept': 'application/json'
}),
withCredentials: true
});
}
reportsUsage = new Map<number, number>();
this.statsService.getReportsUsage().subscribe(data => {
this.reportsUsage = data;
//1
console.log(this.reportsUsage);
//2
console.log(this.reportsUsage.values());
//3
console.log(typeof(this.reportsUsage));
};
Results:
1
{1: 20, 2: 30, 3: 40, 4: 50, 5: 70, 6: 60, 7: 90, 8: 30}
2
ERROR TypeError: this.reportsUsage.values is not a function
3
object
During the conversion from Dictionary to an object data type, I attempted the following which did not work:
console.log(new Map(this.reportsUsage));
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))