Page Redirecting issues Node-js

≯℡__Kan透↙ 提交于 2021-02-11 14:48:28

问题


I'm trying to open places.ejs file on clicking the submit button on show.js page, just like the show.ejs page opens on clicking the submit button on new.ejs file, but a reference error is occurring. Please help me fix the error. I'm attaching herewith my routes.js code and a part of my index.js code Any help would be highly appreciable. Thank you

routes.js

const {
   con,
   sessionStore
 } = require('./config/db');
exports.new = function(req, res){
    message = '';
   if(req.method == "POST"){
      const post  = req.body;
      const username= post.username;
      const title= post.title;
      const state= post.state;
      const category= post.category;
      const description= post.description;
 
      if (!req.files)
                return res.status(400).send('No files were uploaded.');
 
      const file = req.files.uploaded_image;
      var img_name=file.name;
 
         if(file.mimetype == "image/jpeg" ||file.mimetype == "image/png"||file.mimetype == "image/gif" ){
                                 
              file.mv('public/imgs/uploads/'+file.name, function(err) {
                             
               var sql = "INSERT INTO `nt_data`(`username`,`title`,`state`,`category`, `images` ,`description`) VALUES (?,?,?,?,?,?)";
               var query = con.query(sql, [username, title, state, category, img_name, description], function(err) {
                  console.log(err)
                 if (!err) {
                   res.redirect('show/' + category);
                 }
                 else {
                  message = "This format is not allowed , please upload file with '.png','.gif','.jpg'";
                  res.render('new.ejs',{message: message});
                }
               }); 
            }); 
          
   } 
}
   else {
      res.render('new');
   }
 
};
exports.show = function(req, res){
    let message = '';
  
   var category = req.params.category;
    const sql="SELECT * FROM `nt_data` WHERE `category`='"+category+"'"; 
    con.query(sql, function(err, result){
      console.log(err)
     if(result.length <= 0){
     
      message = "show not found!";
      
      res.render('show.ejs',{data:result, message: message});
     }
     else{
      res.redirect('places/'+ username);
    }
   });
};
exports.places = function(req, res){
    let message = '';
  
   var username = req.params.username;
   const sql="SELECT * FROM `nt_data` WHERE `username`='"+username+"'"; 
    con.query(sql, function(err, result){
      
     if(result.length <= 0)
     
      message = "places not found!";
      
      res.render('places.ejs',{data:result, message: message});
   });
};

part of index.js code

app.get('/new', loginRequired, routes.new);//call for main index page
app.post('/', loginRequired, routes.new);//call for signup post 


app.get('/show/:category', loginRequired, routes.show);
app.post('/', loginRequired, routes.show);
app.get('/places/:username', loginRequired, routes.show);

error

ReferenceError: username is not defined
    at Query.<anonymous> (C:\Users\hp\Desktop\Internship\Nt\routes.js:62:31)

回答1:


I think the problem is this: you defined username in exports.new. But when loading exports.places the username will be undefined. So what I suggest is this: pass he username to show like this, /show/:username/:category Call that route from export.new like this: res.redirect('show/' + username + '/' + category) Then from exports.show call places with res.redirect('places/'+ req.params.username);

Let me know if it helps!




回答2:


Please what is the value is variable username holding. I can see you didn't assign any value to it.

  • Create a variable username and assigned the username gotten from the db to it.

However, I changed the redirection path.

exports.show = function(req, res){
    let message = '';
  
   var category = req.params.category;
    const sql="SELECT * FROM `nt_data` WHERE `category`='"+category+"'"; 
    con.query(sql, function(err, result){
      console.log(err)
     if(result.length <= 0){
     
      message = "show not found!";
      
      res.render('show.ejs',{data:result, message: message});
     }
     else{
      // Take a look;
      const username = #sql.username or anyhow you point to a field 
      res.redirect('/places/'+ username);
    }
   });
};

Try that


来源:https://stackoverflow.com/questions/65183218/page-redirecting-issues-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!