Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138279

NodeJs, if statements inside connection.query

$
0
0

i am a c# guy trying to create a login page using in react, node and mysql. i have a doubt that can i write 'if statements inside connection.query'. Below is the code snippet i am using. When i am trying to run this script, i am getting proxy error.

app.post('/api/world', (req, res) => {
 try{
  console.log(req.body);
  uname = req.body.post;

  var DBuserCheck = "SELECT EXISTS (SELECT * FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"' + ")";
  var DBpwdCheck = "SELECT pwd FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"';

connection.query(DBuserCheck, function(err,data){
if(DBuserCheck){
  connection.query(DBpwdCheck, function(err, pdata){
    if(pdata == req.body.password){
      res.json({Successfully_loggedin});
    }
    (err)? res.send(err) : res.json({pwdData_Wrong_Password: pdata});
    console.log('enter valid credentials');        
  });
}
(err) ? res.send(err) : res.json({ USERdata: data });
console.log('user query data: dataSTR',+data +" ; req string "+ req.body.post +"; uname string "+ uname);

 });
}
catch(ex){
  console.log('Message from Catch is ----->>>> '+ex);
}

});

Is my code correct or i have to follow any other procedure. My code first runs correctly and crashes at the ending. I am getting the below error.

PS H:\PyProjects\login\loginproj> yarn dev
yarn run v1.17.3
$ concurrently --kill-others-on-fail "yarn server""yarn client"
$ cd client && yarn start
$ nodemon server.js
$ react-scripts start
[0] [nodemon] 1.19.4
[0] [nodemon] to restart at any time, enter `rs`
[0] [nodemon] watching dir(s): *.*
[0] [nodemon] watching extensions: js,mjs,json
[0] [nodemon] starting `node server.js`
[0] Listening on port 5000
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade caniuse-lite browserslist`
[1] Starting the development server...
[1]
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`
[1] Compiled successfully!
[1]
[1] You can now view create-react-app-express in the browser.
[1]
[1]   Local:            http://localhost:3000/
[1]   On Your Network:  http://10.0.0.65:3000/
[1]
[1] Note that the development build is not optimized.
[1] To create a production build, use yarn build.
[1]
[0] {}
[0] user query data: dataSTR NaN ; req string undefined; uname string undefined
[0] H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\Parser.js:437
[0]       throw err; // Rethrow non-MySQL errors
[0]       ^
[0]
[0] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0]     at ServerResponse.setHeader (_http_outgoing.js:470:11)
[0]     at ServerResponse.header (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:771:10)
[0]     at ServerResponse.send (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:170:12)
[0]     at ServerResponse.json (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:267:15)
[0]     at Query.<anonymous> (H:\PyProjects\login\loginproj\server.js:44:36)
[0]     at Query.<anonymous> (H:\PyProjects\login\loginproj\node_modules\mysql\lib\Connection.js:525:10)
[0]     at Query._callback (H:\PyProjects\login\loginproj\node_modules\mysql\lib\Connection.js:491:16)
        at Query.Sequence.end (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
[0]     at Query._handleFinalResultPacket (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
[0]     at Query.EofPacket (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
[0] [nodemon] app crashed - waiting for file changes before starting...
[1] Proxy error: Could not proxy request /api/world from localhost:3000 to http://localhost:5000/.
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Viewing all articles
Browse latest Browse all 138279

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>