console.log打印返回显示未定义的值

时间:2017-12-07 10:16:30

标签: javascript mysql electron

大家好,我无法想象发生了什么,任何人都可以帮助我 我是电子新手,我需要从数据库(MySQL)中获取值,当我点击console.log(行)时,我收到了对象值,但是当我将行返回到其他函数时,它显示未定义可以任何人帮我修复此

 @ 
       <html>
   <head>
      <meta charset="UTF-8">
     <title>Hello World!</title>
   </head>
    <body>
  <h1>Connecting to MySQL</h1>
  <br>
  <input type="button" id="action-btn" onclick="a1()" value="Retrieve 10 
     first rows in the database" />
  <table id="table" border="1">
     <tbody>
     </tbody>
  </table>
  </body>
  <script>
  function a1()
  {
    var val=a();

    console.log(val);
  }
      var mysql = require('mysql');


      const {app, BrowserWindow} = require('electron')
      const url = require('url')
      const path = require('path')


  function a(){
          var mysql = require('mysql');
          var h = new Object();
          // Add the credentials to access your database
          var connection = mysql.createConnection({
              host     : 'localhost',
              user     : 'root',
              password : null,
              database : 'last'// last is my database 
          });

          // connect to mysql
          connection.connect(function(err) {
              // in case of error
              if(err){
                  console.log(err.code);
                  console.log(err.fatal);
              }
          });

          // Perform a query
          $query = 'SELECT * FROM `customer`';

          connection.query($query, function(err, rows, fields) {
              if(err){
                  console.log("An error ocurred performing the query.");
                  console.log(err);
                  return;
              }

               console.log(rows)// i received the  outputted value
        return rows;       //but when i use return rows to other function
                           // result in undefined
          });

          // Close the connection
          connection.end(function(){
              // The connection has been closed
          });

      }

   </script>
</html>

0 个答案:

没有答案