Struggling to convert XML API response to JSON using xml2js library, facing issues with getting 'undefined' in the console. Here is my API service:
export class WordgameService {
public apiUrl = "http://www.wordgamedictionary.com/api/v1/references/scrabble/";
public apiKey = "********************";
constructor(private http: HttpClient) { }
getSearchTerm(inputValue: string){
var xmlString = this.http.get(this.cors+this.apiUrl + inputValue+this.apiKey,{ responseType: 'text' , headers:{'Content-Type': 'application/xml'}}).subscribe(response => {
console.log(response);
});
var json = this.convertToJson(xmlString)
console.log(json)
return json;
}
convertToJson(xml:any){
var json =xml2js.parseString(xml,function(err,result){})
}
And the component:
export class WordCheckerComponent implements OnInit {
searchword = new FormControl('');
private results :any;
constructor(private apiService: WordgameService) {}
searchWord() {
this.results = this.apiService.getSearchTerm(this.searchword.value);
}
ngOnInit(): void {}
}
Response for the word "test":
<entry>
<word>test</word>
<scrabble>1</scrabble>
<scrabblescore>4</scrabblescore>
<sowpods>1</sowpods>
<sowpodsscore>4</sowpodsscore>
<wwf>1</wwf>
<wwfscore>4</wwfscore>
</entry>