I am returning data from API but when I try to parse it as a JSON in Angular ts it says "Argument of type 'Subscription' is not assignable to parameter of type 'string'", what I want to do is to get the json response from API as [{name:"name1"},{name:"name2"}] rather than [{"name":"name1"},{"name":"name2"}] which doesnt write the name on csv file
[HttpGet("getEmployees")]
[ProducesResponseType(200, Type = typeof(IEnumerable<EmployeesView>))]
public async Task<IActionResult> GetEmployeesByCreateDate(DateTime period)
{
try
{
// read model returns users by a logic as enumerable
return Ok(await _readModel.GetEmployees(period));
}
catch (Exception e)
{
// throw exception
}
myService.ts
getAllPosts() {
return this.http.get<IEmployee[]>(this.URL).subscribe((data: any) => {return data;});
}
download(data, filename='data') {
let csvData=this.ConvertToCSV(data, ['name1','name2']);
let blob = new Blob(['\ufeff' + csvData],{type:'text/csv;charset=utf8;'});
let dwldLink = document.createElement("a");
let url = URL.createObjectURL(blob);
dwldLink.setAttribute("href", url);
dwldLink.setAttribute("download", filename + ".csv");
dwldLink.style.visibility = "hidden";
document.body.appendChild(dwldLink);
dwldLink.click();
}
ConvertToCSV(objArray, headerList) {
let array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
let str = '';
let row = 'S.No,';
for (let index in headerList) {
row += headerList[index] + ',';
}
row = row.slice(0, -1);
str += row + '\r\n';
for (let i = 0; i < array.length; i++) {
let line = i + 1 + '';
for (let index in headerList) {
let head = headerList[index];
line += ',' + array[i][head];
}
str += line + '\r\n';
}
return str;
}
myComponent.ts
JsonData = JSON.parse( this.URL.getAllPosts());
download2(){ this.URL.download(this.JsonData, 'jsontocsv'); } //here is the problem
I want to return my data as :
[
{
name:"name1",
surname:"surname1"
},
{
name:"name2",
surname:"surname2"
}
]
But instead I get it like this (I need it in format as above becuase I am downloading the data I get as CSV)
[
{
"name":"name1",
"surname":"surname1"
},
{
"name":"name2",
"surname":"surname2"
}
]