我正在使用Node JS,并且尝试连接到mysql数据库。它一直保持断开连接。当我运行npm start时,错误显示如下


  D:\ face-api.js \ face-api.js \ examples \ FaceLogInBackend \ app.js:26
  app.use(bodyParser.json()); //支持JSON编码的主体
      ^
  
  TypeError:无法读取未定义的属性“ use”
  在对象。 (D:\ face-api.js \ face-api.js \ examples \ FaceLogInBackend \ app.js:26:5)
  在Module._compile(内部/模块/cjs/loader.js:776:30)
  在Object.Module._extensions..js(内部/模块/cjs/loader.js:787:10)


app.js中的代码如下

var mysql      = require('mysql');
var connection = mysql.createConnection({
host     : 'localhost',
user     : 'root',
password : '',
database : 'bookstore'

});

connection.connect(function(err) {
if (err) throw err
console.log('You are now connected...')
})

最佳答案

你需要这个

var mysql      = require('mysql');

// instead of storing the result in a variable, just make the connection,
mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'bookstore'
});

// now connect to mysql
mysql.connect(function(err) {
    if (err) throw err
    console.log('You are now connected...')
})

10-07 19:33
查看更多