Node JS, MYSQL connection.query 不是函数
Node JS, MYSQL connection.query is not a function
db_config.js:
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'test'
})
connection.connect(function(err){
if(!!err){
console.log(err)
}
else{
console.log('Connected')
}
})
module.export = connection
Users.js
var express = require('express');
var router = express.Router();
var connection = require('../db_config')
/* GET users listing. */
router.get('/comments', function(req, res, next) {
var getCommentsQ = "SELECT * FROM `comments`";
connection.query(getCommentsQ, function(err, result){
if(err){
console.log(err)
res.send('Unable to get the comments')
}
else{
res.send(result)
}
})
});
module.exports = router;
错误:每当我转到“localhost:3000/users/comments”时,它都会说:“connection.query 不是一个函数”
我遵循了这个教程:I was following this tutorial, his worked mine not working
警告他使用 Jade im 使用 Ejs,但我认为这不是视图引擎问题
您的导出有错字,请尝试像这样导出您的 connection
。
module.exports = connection;
db_config.js:
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'test'
})
connection.connect(function(err){
if(!!err){
console.log(err)
}
else{
console.log('Connected')
}
})
module.export = connection
Users.js
var express = require('express');
var router = express.Router();
var connection = require('../db_config')
/* GET users listing. */
router.get('/comments', function(req, res, next) {
var getCommentsQ = "SELECT * FROM `comments`";
connection.query(getCommentsQ, function(err, result){
if(err){
console.log(err)
res.send('Unable to get the comments')
}
else{
res.send(result)
}
})
});
module.exports = router;
错误:每当我转到“localhost:3000/users/comments”时,它都会说:“connection.query 不是一个函数”
我遵循了这个教程:I was following this tutorial, his worked mine not working
警告他使用 Jade im 使用 Ejs,但我认为这不是视图引擎问题
您的导出有错字,请尝试像这样导出您的 connection
。
module.exports = connection;