refresh page automatically on form submission in nodejs

Viewed 129

I have a script where I am calling my nodejs function from the front end, and then performing my sql query, like so:

  <script type="text/javascript">  
                var myWidget = cloudinary.createUploadWidget({
                  cloudName: 'xxx', 
                  uploadPreset: 'XS'}, (error, result) => { 
                    if (!error && result && result.event === "success") { 
                      console.log('Done! Here is the image info: ', result.info); 
                      console.log(result.info.secure_url)
                      var result_url = result.info.secure_url;
                      console.log("result url is " + result_url)
                      document.getElementById("url").value = result_url;
                      var employee_num = document.getElementById('employee_num').value;
                     
                      fetch('/changeProfileImage', {
                            method: 'POST', 
                            headers: {'Content-Type': 'application/json'},
                            body: JSON.stringify({
                                result_url,
                                employee_num
                            })
                        })
                        
                    }
                  }
                )

works great and all, send query to backend, which performs this:

    app.post('/changeProfileImage', (req, res) => {
  var employee_num = req.body.employee_num;
  var url = req.body.result_url;
  console.log("e " + employee_num)
  console.log("u " + url)
  
  var changeProfileImage = "update EMPLOYEES set (PROFILE_IMAGE)= '" + url + "' where EMPLOYEE_NUM = '" + employee_num + "'";
  ibmdb.open(ibmdbconnMaster, function (err, conn) {
    if (err) return console.log(err);
    conn.query(changeProfileImage, function (err, rows) {
      if (err) {
        console.log(err);
      }
      
      res.redirect(req.get('referer'));

      conn.close(function () {
    // console.log("closed the function /index");
     });
  })
})
})

however, for some reason, when it calls the function, although everything updates and the sql query is successfully performed, it doesn't refresh page / redirect back to the page like I want it to ... I've tried res.render('index.ejs'), and res.redirect('/index') as well, but nothing refreshes the page like I need to actually show the updated data. Probably something so small and stupid, but can't seem to get it! plz help :)

0 Answers
Related