微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据没有被推送到mysql表中

如何解决数据没有被推送到mysql表中

connection.query("SELECT email,password FROM authenticate where email=?",email,function (error,result) {
      if (error) {
        throw error;
      } else {
        if (result.length == 0) {
          console.log('name not available');
          res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
        } else if (result[0].password == password) {
          console.log(userData);
          connection.query("INSERT INTO userapplications SET ? ",userData,result,fields) {
              if (error){
                throw error;
              } 
              else{
                console.warn("insertion successful");
                res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
              }   
            });
        } else {
          console.log('invalid user and password');
          res.sendFile(path.join(__dirname + "/public/apply.html"))
        }
      }
    });
  connection.end();
错误

调用quit后不能入队查询

由于某种原因数据库连接,数据没有添加到表中。 我是初学者。

解决方法

我认为这是因为 NodeJS 的异步特性。尝试将 connection.end() 与 connection.query() 连接起来。

喜欢:

connection.query("your_code_here).end();
,

将您的 connection.end(); 移动到回调中,如下所示。

connection.query("SELECT email,password FROM authenticate where email=?",email,function (error,result) {
      if (error) {
        connection.end();
        throw error;
      } else {
        if (result.length == 0) {
          console.log('name not available');
          res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
        } else if (result[0].password == password) {
          console.log(userData);
          connection.query("INSERT INTO userapplications SET ? ",userData,result,fields) {
              if (error){
                connection.end();
                throw error;
              } 
              else{
                console.warn("insertion successful");
                res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
                connection.end();
              }   
            });
        } else {
          console.log('invalid user and password');
          res.sendFile(path.join(__dirname + "/public/apply.html"))
          connection.end();
        }
      }
    });
  connection.end();

像这样,你可以做出改变。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。