2

I'm trying to run a route live with query using nodejs express app in render.com. All other routes related to the crud operations are getting executed except the search filter route url with the query. I'm getting the below error while running it in render.com

browser_error_screen

I'm passing this parameter correctly in my live url => https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/?quer=hi, but its throwing the internal server error. If you check the https://stackoverflow-demo-nodejs-express-s.onrender.com/sd-db-1021/collections/products/, it would give the response for the same router.get method implemented in the storage.js file.

Moreover, the operation is getting executed with localhost. Below is the reference:

enter image description here

I'm unable to debug this issue. Any help regarding the issue would be appreciated.

Here's my piece of code:

/* app.js */
const express = require('express');
const bodyParser = require('body-parser');

// create express app
const app = express();
const port = process.env.PORT || 3000;
const cors = require('cors');

// set middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

// import routes
const routes = require('./routes/Routes');
app.use('/', routes);

//start server
app.listen(port, () => {
    console.log(`Server is up on port: ${port}`);
});


/* storage.js */
storageRoutes.get(storageUrl, (req, res) => {
    const { type } = req.params;
    const { quer } = req.query;
    // get existing data collection// fetch the collection from the storage related to the products
    let existingDataCollection = getData(storagePath);
    // get the existing data-set fot the specific type
    let existingCollectionTypeData = [...existingDataCollection[type]];

    // search filter implementation based on query
    if (quer) {
        // filter query
        const query = quer.toLowerCase().replaceAll(" ", "");
        // get the data-object related to the specified type
        existingCollectionTypeData = existingCollectionTypeData.filter((element) =>
            element.name.replaceAll(" ", "").toLowerCase().includes(query)
        );
    }

    // send the response with the collection of specified types
    res.send(existingCollectionTypeData);
});
Subhojit
  • 1,389
  • 8
  • 19
  • 33
  • 1
    Very strange. Which is the node version on Render? Is it different from yours? – pierpy Jul 23 '23 at 08:52
  • @pierpy Thanks a lot for the comment. The issue was render.com is using the `node version 14.17.0` and the node version I was using is `18.16.1`. The issue is resolved and I have posted the solution down below. – Subhojit Jul 23 '23 at 09:35

1 Answers1

0

Finally, I got the issue resolved.

Firstly, Thanks a lot @pierpy. I found the solution after taking your comment a bit serious.

The issue is => render.com is using the node version 14.17.0 and the node version I was using was 18.16.1.

Identified from the logs:

enter image description here

Hence, my query implementation which had the below code was getting failed because the node version 14.17.0 was not able to recognize .replaceAll(" ", "") from the below code, because in the node version 14.17.0 the implementation of .replaceAll might not be present:

// search filter implementation based on query
if (quer) {
    // filter query
    const query = quer.toLowerCase().replaceAll(" ", "");
    // get the data-object related to the specified type
    existingCollectionTypeData = existingCollectionTypeData.filter((element) =>
        element.name.replaceAll(" ", "").toLowerCase().includes(query)
    );
}

Hence I followed the below steps to get to the solution:

  1. Removed the => node_modules and package-lock.json with the help of rm -rf <folder/filename> command (applicable for git bash in windows)
  2. Downgraded the node version to 14.17.0
  3. npm install
  4. replaced => .replaceAll(" ", "") with replace(/\s/g,'')
  5. pushed the code and deployed the repo back.

If any of you have encountered the same issue with render.com by deploying the node-express app, you can consider this approach and get rid of the issue.

Subhojit
  • 1,389
  • 8
  • 19
  • 33