Currently, I am facing an issue with my Angular service called ConnexionService. The problem arises when I try to incorporate CSV files into this service using HttpClient. Strangely, the component associated with this service fails to display once HttpClient is added to the constructor. Surprisingly, everything works perfectly fine when HttpClient is omitted from the setup. Can anyone guide me on how to successfully retrieve information from CSV files without disrupting the functionality of the webpage? My sincere apologies for any confusion caused. Below is the code snippet for ConnexionService:
import { Injectable } from '@angular/core';
import { Auth, User, authState, signOut, signInWithPopup, GoogleAuthProvider } from '@angular/fire/auth';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ConnexionService {
constructor(private fbAuth: Auth, private http : HttpClient) { }
readonly obsUser: Observable<User | null> = authState(this.fbAuth);
async logout(): Promise<void> {
return signOut(this.fbAuth);
}
async loginGoogle(): Promise<void> {
return signInWithPopup(this.fbAuth, new GoogleAuthProvider()).then(
(result) => {
const credential = GoogleAuthProvider.credentialFromResult(result);
let token: string | undefined;
if (credential) {
token = credential.accessToken;
} else {
console.error("La credential est null.");
}
const user = result.user;
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const email = error.customData.email;
const credential = GoogleAuthProvider.credentialFromError(error);
}
)
}
lireLivreur(csvData:string){
const lines=csvData.split('\n');
const result:Livreur[]= [];
const headers=lines[0].split(',');
for(let i=1;i<lines.length;i++){
const obj: ParsedData = {};
const currentLine=lines[i].split(',');
for(let j=0;j<headers.length;j++){
obj[headers[j]]=currentLine[j];
}
const livreur:Livreur={
trigramme:obj[headers[0]],
prenom:obj[headers[1]],
nom:obj[headers[2]],
photo:obj[headers[3]],
telephone:obj[headers[4]],
emploi:obj[headers[5]],
entrepot:obj[headers[6]],
tournees:obj[headers[7]],
}
if(livreur.emploi=='livreur'){
result.push(livreur);
}
}
return result;
}
lireInfos(){
this.http.get('./assets/Export_Employés.csv',{responseType:'text'})
.subscribe(data=>{
this.livreurs=this.lireLivreur(data);
},
);
}
livreurs : Livreur[]=[];
livreur: Livreur = {
trigramme: '',
prenom: '',
nom: '',
photo: '',
telephone: '',
emploi: '',
entrepot: '',
tournees: ''
};
}
interface Livreur {
trigramme: string;
prenom: string;
nom: string;
photo: string;
telephone: string;
emploi: string;
entrepot: string;
tournees: string;
}
interface ParsedData {
[key: string]: string;
}
Below is the code snippet for ConnexionComponent:
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ConnexionComponent {
constructor(private cox: ConnexionService){
}
async logGoogle():Promise<void>{
this.cox.loginGoogle();
}
async logOut():Promise<void>{
this.cox.logout();
}
}
It's strange that even before injecting my Connexion service into my component, it already faces issues. This leads me to believe that there might be a problem within the ConnexionService itself.