I have a new project that involves working with a large dataset of dummy information. The data is stored in a csv file located in the assets folder, and it consists of over 200 lines. My task is to read the file without needing to make any changes to it.
My main objective is to extract each line from the file, parse it into a class, and then store it in a map. However, this parsing and mapping process is not causing any issues for me at the moment.
The technology I am using for this project is Angular version 17.1.3. Since I am operating in standalone mode, there is no app.module.ts file included in my setup. This fact has been highlighted in various troubleshooting guides I've come across. Additionally, no other dependencies or modules have been imported into the project as of yet.
Initially, I attempted to utilize httpClient for reading the file but encountered problems related to its provision:
import { Injectable } from '@angular/core';
import { Company } from './company';
import { HttpClient } from '@angular/common/http';
import { IDirectoryService } from './directory.service.interface';
@Injectable({
providedIn: 'root'
})
export class DirectoryService implements IDirectoryService{
directory = new Map();
constructor(private http:HttpClient){
const fileName = "testData.csv";
this.http.get('assets/'+fileName, {responseType: 'text'}).subscribe(data => console.log(data));
}
public getCompany(id:string):Company{
return this.directory.get(id);
}
public getCount():Number{
return this.directory.size;
}
}
ERROR Error [NullInjectorError]: R3InjectorError(Standalone[_AppComponent])[_DirectoryService -> _DirectoryService -> _HttpClient -> _HttpClient]:
NullInjectorError: No provider for _HttpClient!
As an alternative approach, I tried importing the file directly but ran into a different issue:
import * as data from '../../../assets/testData.csv';
Cannot find module '../../../assets/testData.csv' or its corresponding type declarations.
It seems overly complicated just to import a basic csv file. Surely, it shouldn't be this challenging. If necessary, I can convert the csv file to text format to simplify the process.
I followed several online tutorials initially, which led me to encounter errors. Subsequently, as I searched for solutions online, most references directed me towards adding an app.module.ts file – a component that appears to be obsolete in newer versions of Angular. It's frustrating to run into roadblocks when trying to solve a seemingly straightforward issue, especially when existing solutions are either outdated or irrelevant to my specific problem.