简体   繁体   中英

Node-Express: filter request query and display rest of the data

I am new in Node query. I am using Node express for my backend app. I have one nested json which has three language options and inside language there are auth , dashboard , data , and data1 options. I want to filter the query and display rest of the json data in my browser. for example if I type url like this: http://localhost:5000/?namespaces=auth&languages=en,fi then it will display language en and fi's data and from namespaces I want to display auth's data. For display the data I have created one output empty object and want to add it in my output object. but don't know how to do that.

I have share my code in codesandbox .

This is my json data

{
    "en": {
        "auth": {
            "welcomeMessage3": "Hi John"
        },
        "dashboard": {
            "welcomeMessage": "Hi Doe"
        },
        "data (1)": {
            "welcomeMessage3": "Hi Jonny"
        },
        "data": {
            "welcomeMessage3": "Hi Monty"
        }
    },
    "fi": {
        "auth": {
            "welcomeMessage3": "Moi name "
        },
        "dashboard": {
            "welcomeMessage": "Moi dashboard"
        },
        "data (1)": {
            "welcomeMessage3": "Moi data 1"
        },
        "data": {
            "welcomeMessage3": "Moi data"
        }
    },
    "sv": {
        "auth": {
            "welcomeMessage3": "Hej John"
        },
        "dashboard": {
            "welcomeMessage": "Hej dashboard"
        },
        "data (1)": {
            "welcomeMessage3": "Hej data"
        },
        "data": {
            "welcomeMessage3": "Hej data"
        }
    }
}

This is my express app

    const express = require('express')
    const app = express()
    const port = 5000
    const translationData = require('./translations'); // My json
    
    
    const filterTranslations = (namespaces, languages) => {
  let output = {};
  const translations = { ...translationData };
  console.log("I am typing Languages", languages);
  console.log("I am typing namespace", namespaces);
  for (const lng in translations) {
    console.log("languages are coming from translation json", lng);
    if (lng.length !== 0 && lng !== languages) {
      delete translations[lng];
      console.log("Delete rest of the language", lng);
    } else {
      for (const ns in translations[lng]) {
        if (ns.length !== 0 && ns !== namespaces) {
          delete translations[lng][ns];
          console.log("delete rest of the Namespace", ns);
        }
      }
    }
  }
  return output;
};
    
    
    app.get('/', (req, res) => {
      res.send(
        filterTranslations(
          req.query.namespaces,
          req.query.languages,
        )
      )
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })

There are several mistakes :

  • the function filterTranslations should return a value, here translations ,
  • the conditions in your if statement is not correct : lng !== languages , in your example, languages = 'en,fi' and lng will be en or fi . Look at String.includes( searchString [, position ]) or split your languages and use Array.includes( searchElement[, fromIndex]) .

Hoping I helped you, have a good day !

If I understand correctly what you are trying to do I think you overcomplicate things.

First of all passing a comma separated string as a parameter is not the best idea as you would have to turn it into an array in the backend by splitting the string in the comma. It's best if it's an array to begin with. That means that your url would be :

http://localhost:8080/?namespaces=auth&languages[]=en&languages[]=fi

Then the function that handles this would be:

const filterTranslations = (namespaces, languages) => {
  let output = [];
  const translations = { ...translationData };
  //languages are now an array
  for (let l in languages) {
    let lng = languages[l];
    // for each language you send as a parameter you push a new object in your output with the language as a key and the values
    output.push({[lng]:translations[lng]});
  }
  return output;
};

The expected output is the following:

[
    {
        "en": {
            "auth": {
                "welcomeMessage3": "Hi John"
            },
            "dashboard": {
                "welcomeMessage": "Hi Doe"
            },
            "data (1)": {
                "welcomeMessage3": "Hi Jonny"
            },
            "data": {
                "welcomeMessage3": "Hi Monty"
            }
        }
    },
    {
        "fi": {
            "auth": {
                "welcomeMessage3": "Moi name "
            },
            "dashboard": {
                "welcomeMessage": "Moi dashboard"
            },
            "data (1)": {
                "welcomeMessage3": "Moi data 1"
            },
            "data": {
                "welcomeMessage3": "Moi data"
            }
        }
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM