简体   繁体   中英

not rendering page on request node.js

I have this front-end function:

 var finalSelValue = ""
    function changeFunc() {
     var selectBox = document.getElementById("selectBox");
     var selectedValue = selectBox.options[selectBox.selectedIndex].value;

   finalSelValue = selectedValue
    fetch('/adminFilter', {
        method: 'POST',
        headers: {'content-type': 'application/json'},
        body: JSON.stringify({
            finalSelValue
        })
    })
    }

and then this on the backend:

  router.post('/adminFilter', auth.ensureAuthenticated, auth.roleCheck('ADMIN'), (req, res) => {

  console.log("filter " + req.body.finalSelValue)

  var query = "select * from tkwdottawa where STATUS = '" + req.body.finalSelValue + "'"
  ibmdb.open(DBCredentials.getDBCredentials(), function(err, conn) {
      if (err) return console.log(err);
      conn.query(query, function(err, rows) {
          if (err) {
              res.writeHead(404);

          }

          for (var i = 0; i < rows.length; i++) {
            console.log(rows[i])
          }

          res.render('AdminDash', {
              page_title: "AdminDash",
              data: rows
          });


          conn.close(function() {
              console.log('done adminFilter');

          });
      });
  });


})

it is logging the new values properly, where the loop is, but it is not re-rendering the page with the new data! How do I fix this!

The issue might be because you have not fully configured your nodejs application. You should be aware that the render() method must have a templating engine that handles views to be available. You can just install one of the below templating engine ie

  1. Pug
  2. EJS
  3. Handlebars

from Npm repository.

Just use npm install [pug or ejs] i suggest this two because they have very easy learning curve

Another solution is to just use send() or sendFile() methods. This will send back the data to the client in this case the fetch request, then you can get the response from the .then callback. and update the view from front end script.

Just a note

Render method is used with a templating engine and mostly is a server side rendering oriented approach

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