Javascript connection.query中的NodeJs、if语句

Javascript connection.query中的NodeJs、if语句,javascript,mysql,node.js,Javascript,Mysql,Node.js,我是一个c#家伙,试图使用react、node和mysql创建登录页面。我怀疑我是否可以在connection.query中编写“if语句”。下面是我正在使用的代码片段。当我试图运行此脚本时,我遇到代理错误 app.post('/api/world', (req, res) => { try{ console.log(req.body); uname = req.body.post; var DBuserCheck = "SELECT EXISTS (SELECT * FR

我是一个c#家伙,试图使用react、node和mysql创建登录页面。我怀疑我是否可以在connection.query中编写“if语句”。下面是我正在使用的代码片段。当我试图运行此脚本时,我遇到代理错误

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);
}

});
我的代码是否正确,或者我必须遵循任何其他程序。我的代码首先正确运行,最后崩溃。我得到下面的错误

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).
PS H:\PyProjects\login\loginproj>dev
纱线运行v1.17.3
$并发--在失败的“纱线服务器”“纱线客户端”上杀死其他人
$cd客户端和开始(&S)
$nodemon server.js
$react脚本启动
[0][nodemon]1.19.4
[0][nodemon]要随时重新启动,请输入'rs'`
[0][nodemon]正在监视目录:**
[0][nodemon]观看扩展:js、mjs、json
[0][nodemon]正在启动`node server.js`
[0]正在端口5000上侦听
[1] 浏览器列表:caniuse lite已过时。请运行下一个命令“使用lite浏览器列表”`
[1] 正在启动开发服务器。。。
[1]
[1] 浏览器列表:caniuse lite已过时。请运行下一个命令“纱线升级”`
[1] 浏览器列表:caniuse lite已过时。请运行下一个命令“纱线升级”`
[1] 编译成功!
[1]
[1] 现在,您可以在浏览器中查看create react app express。
[1]
[1] 本地:http://localhost:3000/
[1] 在您的网络上:http://10.0.0.65:3000/
[1]
[1] 请注意,开发构建没有经过优化。
[1] 要创建生产构建,请使用纱线构建。
[1]
[0] {}
[0]用户查询数据:dataSTR-NaN;req字符串未定义;uname字符串未定义
[0]H:\PyProjects\login\loginproj\node\u modules\mysql\lib\protocol\Parser.js:437
[0]抛出错误;//重新显示非MySQL错误
[0]       ^
[0]
[0]错误[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头
[0]位于ServerResponse.setHeader(_http_outgoing.js:470:11)
[0]位于ServerResponse.header(H:\PyProjects\login\loginproj\node\u modules\express\lib\response.js:771:10)
[0]位于ServerResponse.send(H:\PyProjects\login\loginproj\node\u modules\express\lib\response.js:170:12)
[0]位于ServerResponse.json(H:\PyProjects\login\loginproj\node\u modules\express\lib\response.js:267:15)
[0]在查询时。(H:\PyProjects\login\loginproj\server.js:44:36)
[0]在查询时。(H:\PyProjects\login\loginproj\node\u modules\mysql\lib\Connection.js:525:10)
[0]在查询时。\u回调(H:\PyProjects\login\loginproj\node\u modules\mysql\lib\Connection.js:491:16)
在Query.Sequence.end(H:\PyProjects\login\loginproj\node\u modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
[0]在查询时。\u handleFinalResultPacket(H:\PyProjects\login\loginproj\node\u modules\mysql\lib\protocol\sequences\Query.js:139:8)
[0]位于Query.EofPacket(H:\PyProjects\login\loginproj\node\u modules\mysql\lib\protocol\sequences\Query.js:123:8)
[0][nodemon]应用程序崩溃-正在等待文件更改,然后再启动。。。
[1] 代理错误:无法将请求/api/world从localhost:3000代理到http://localhost:5000/.
[1] 看https://nodejs.org/api/errors.html#errors_common_system_errors 有关更多信息,请参阅。

根据代码中的错误多次发送响应,下面是您的工作代码

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(err) {
                res.send(err) : res.json({ USERdata: data })
                console.log('user query data: dataSTR', +data + " ; req string " + req.body.post + "; uname string " + uname);
            } else {
                connection.query(DBpwdCheck, function (err, pdata) {
                    if (pdata == req.body.password) {
                        res.json({ Successfully_loggedin });

                    } else {
                        (err) ? res.send(err) : res.json({ pwdData_Wrong_Password: pdata });
                        console.log('enter valid credentials');
                    }
                });
            }
        });
    }
    catch (ex) {
        console.log('Message from Catch is ----->>>> ' + ex);
    }

});

if
条件在
connection.query的回调函数内,这是可以的。代理错误表示连接出现问题。您可以添加得到的确切错误吗?添加try-catch块并发布错误消息error message:Proxy error:cannot Proxy request/api/world from localhost:3000 to。有关更多信息,请参阅(EconRefused)。如果我只使用一个数据库查询执行相同的操作,我就能够在客户端和服务器之间进行交互。当我添加第二个查询时,得到这个问题(上面的错误消息),这里是单个查询执行的链接。-->谢谢你的回复。我已经用这个代码执行了。运行良好。但是,即使我给出了正确的密码,密码也无法验证。你能帮我一下吗?你能试着查一下pdata变量吗?也许你的数据库密码在pdata里面。pwd之类的东西,或者只是分享pdata的结果,这样我自己就知道了。我试着打印pdata变量,我没有定义。首先你必须检查你的查询,也许你的查询中有一些问题