0

I have a NodeJS app that displays some tables from a database. The view engine is ExpressJS.

On the app I have a dropdown list where u can select the table you want to display. The problem is that any given table that I want to display is only accessible through the dropdown, but what I'm trying to do is make them work through URLs.

So for example when u enter an address like: http://localhost:6789/tableOne, I want the table one to be displayed. But if I load table two from the dropdown, and after that I enter the address I mentioned above, the page displays also table two, so basically is not refreshing with the new data. This makes the app working only through the interface, but I want to share to somebody else the direct link to a table and I can't. (they will receive the last table that was loaded on the app)

To rephrase, take as example an app that displays on the page the route's name. Mine doesn't update.

The routes are dynamically generated and here's an example of the routing that I did:

app.get('/someTables/:' + getNameFromID(selectedID), (req, res) => {
var url = req.url;

for (i in endData) {
    if (i.name == url.substr(10)) {
        selectedID = i.id;
    }
}
res.render('index', { frontData2: endData2, frontData: endData, jsonToDisplayFrontEnd: jsonToDisplay, m1: m1, m21: m21, m22: m22, m1FrontEnd: m1ToDisplay, m21FrontEnd: m21ToDisplay, m22FrontEnd: m22ToDisplay, versionToDisplayFrontEnd: versionToDisplay, titleName: getNameFromID(selectedID), folderPath: getFolderPath(selectedID), folderPath2: getFolderPath2(selectedID) });

});

The function getNameFromID() returns a string.

How can I make this work? Thank you!

  • You can either render the page with the preselected table on the backend or you can read the URL on the frontend and select the table depending on the URL. – jabaa Aug 13 '22 at 15:52
  • When do you think `getNameFromID` gets called, and with what? – jonrsharpe Aug 13 '22 at 15:52
  • 1
    What is `for (i in endData) { ... }` trying to do? This code just looks wrong. `i` is undeclared (making it an implicit global) and it will be a property name or index, not a property value. If `endData` is an array, [NEVER](https://stackoverflow.com/questions/22754315/for-loop-for-htmlcollection-elements/22754453#22754453) use `for/in` to iterate an array. If it's an object, then you're not getting the value, you're just getting the property name. – jfriend00 Aug 13 '22 at 16:13

0 Answers0