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
用戶.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 不是函式”
我遵循了本教程:我正在遵循本教程,他的作業我的不作業
警告他使用 Jade im 使用 Ejs,但我認為這不是視圖引擎問題
uj5u.com熱心網友回復:
您的匯出中有錯字,請嘗試connection像這樣匯出。
module.exports = connection;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/476813.html
標籤:javascript mysql 节点.js
