Search code examples
angularexpressparameterstokenize

Modify a query parameter: express JS get request


I am sending a request from an angular component to an Express server. Everything works fine (I get the results expected and they are displayed correctly).

The moment I try to modify the QUERY paramater, nothing is being displayed. As an example, let's say a user searches the word: 'hands'. I want to tokenize the word to make it singular.

this is the server side implementation:

app.get('/api/words', function(req, res) {
  var name = req.query.name;
  var option = req.query.option;  

  // english word:
  if(option == 1){
    // This is where I check if the word is plural to turn it into singular form
    if(name[name.length - 1]== 's'){
      name = name.slice(0, name.length - 1);
    }

    fs.readFile('words.xml', 'utf-8', function (err, data){
      if(err){

      } else{
        parser.parseString(data, function (err, result) {
        let words = result['entry']['form'];
        str= words.filter(x => x.orth == name);
  }

      res.send(str);

The angular component calling the http service is:

 getWordList(name: string){
    this.spelling = name.toString().toLowerCase();
    this.webservice.getWords(this.spelling, this.selected)
    .subscribe((res: Array<Word>)=> {
      this.elements = res.filter(d=> d.orth == this.spelling || d.asuddimIsem == this.spelling);

      this.dataLoaded.emit(this.elements);
      this.webservice.setData(this.elements);
      this.router.navigate(['/words']);
    })
  }

The data is an xml file with a list of english words. The query searches for the singular form of the word. So if it is a plural word, the tokenization should retrieve the correct item.

When I send the request with 'hands' for example, the console shows a status-code 200, with the correct response. Yet nothing is displayed.


Solution

  • In angular you filter the result using this.spelling, and to what I understand this.spelling contains the word you search (which is hands) and d.orth contains the singular form.

    As of that the filtering will remove every element of the client side.

    How to solve that correctly depends on your usecase. E.g. the singular to filter on the client-side or remove the client side filtering, because you already do that server-side.