我正在尝试显示MySql表'产品'中的数据,但似乎无法正常工作,因为它说该表在数据库中不存在。我已经检查了数据库,它看起来不错,没有语法错误。这给了我这个错误:
code: 'ER_NO_SUCH_TABLE',
errno: 1146,
sqlMessage: 'Table \'storage_manager.products\' doesn\'t exist',
sqlState: '42S02',
index: 0,
sql: 'SELECT * FROM products'
这是我到目前为止所做的:
数据库连接
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'mypass',
port: '3305',
database: 'storage_manager'
});
connection.connect((err) =>{
if(err) {
console.log(err);
} else {
console.log('DB connected');
}
});
module.exports = connection;
js文件
const express = require('express');
const router = express.Router();
const connection = require('../src/dbConnection');
router.get('/', (req, res) => {
var products= [];
connection.query('SELECT * FROM products', (err, rows, fields) => {
if(err) {
console.log(err);
} else {
for(var i = 0 ; i < rows.length; i++) {
var product = {
'id': rows[i].id,
'name': rows[i].product_name,
'quantity': rows[i].quantity,
'value': rows[i].product_value,
'description': rows[i].description
}
// add the product to the product list
products.push(product);
}
res.render('frontpage', {"products": products});
}
});
});
module.exports = router;
非常感谢。