Javascript Can';不执行来自其他文件的查询(connection.query不是函数)
我是node.js和数据库中的新成员。试图执行mysql查询,但我遇到了以下错误Javascript Can';不执行来自其他文件的查询(connection.query不是函数),javascript,mysql,node.js,express,Javascript,Mysql,Node.js,Express,我是node.js和数据库中的新成员。试图执行mysql查询,但我遇到了以下错误TypeError:connection.query不是一个函数 Im使用Node MySQL 2()包 app.js文件中的连接和查询工作正常,但在另一个文件中会抛出该错误 我的代码有什么问题 app.js const chalk = require('chalk'); const debug = require('debug')('app'); const morgan = require('morgan');
TypeError:connection.query不是一个函数
Im使用Node MySQL 2()包
app.js文件中的连接和查询工作正常,但在另一个文件中会抛出该错误
我的代码有什么问题
app.js
const chalk = require('chalk');
const debug = require('debug')('app');
const morgan = require('morgan');
const path = require('path');
const mysql = require('mysql2');
const app = express();
const port = process.env.PORT || 3000;
const connection = mysql.createConnection({
host: '...',
user: '...',
password: '...',
database: '...',
});
connection.connect((err) => {
if (err) {
debug(`error connecting: ${chalk.red(err.stack)}`);
return;
}
debug(`connected as id ${chalk.green(connection.threadId)}`);
});
app.use(morgan('tiny'));
app.use(express.static(path.join(__dirname, '/public/')));
app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css/')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js/')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist/')));
app.set('views', './src/views');
app.set('view engine', 'ejs');
const nav = [{ link: '/books', title: 'Books' },
{ link: '/authors', title: 'Authors' }];
const bookRouter = require('./src/routes/bookRoutes')(nav);
app.use('/books', bookRouter);
app.get('/', (req, res) => {
res.render(
'index',
{
nav,
title: 'Library app',
},
);
});
app.listen(port, () => {
debug(`listening on port ${chalk.green(port)}`);
});
module.exports = connection;
bookRoutes.js
const mysql = require('mysql2');
const debug = require('debug')('app:bookRoutes');
const connection = require('../../app');
const bookRouter = express.Router();
function router(nav) {
const books = [
{
title: 'Nocturna',
genre: 'Fantasy',
author: 'Maya Motayne',
read: false,
},
{
title: 'Finale',
genre: 'Fantasy',
author: 'Stephanie Garber',
read: false,
},
];
bookRouter.route('/')
.get((req, res) => {
connection.query('SELECT * FROM `books`', (error, results) => {
debug(results);
res.render(
'bookListView',
{
nav,
title: 'Library app',
books,
},
);
});
});
bookRouter.route('/:id')
.get((req, res) => {
const { id } = req.params;
res.render(
'bookView',
{
nav,
title: 'Library app',
book: books[id],
},
);
});
return bookRouter;
}
module.exports = router;
编辑:项目树
├── app.js
├── package-lock.json
├── package.json
├── public
| ├── css
| | └── styles.css
| └── js
└── src
├── routes
| └── bookRoutes.js
└── views
├── bookListView.ejs
├── bookView.ejs
└── index.ejs
谢谢你的帮助。bookrouts.js
const mysql = require('mysql2');
const debug = require('debug')('app:bookRoutes');
const connection = require('./../../app').connection; // identify the exported module
// the rest…
现在出现此错误
TypeError:无法读取未定义的属性“query”
是否确定app.js
文件的相对路径正确?它可能需要是:/。/../app
?如果您想编辑您的帖子并添加文件夹/文档树,我们可以确认。(用于快速打印树的便捷模块:)完成后,我在POST中添加了项目树非常好,谢谢。我相信你会想要领先的单周期和斜线在你的道路上。例如,请参见我编辑的答案。也不确定您是否仍然需要。连接
。我已经有一段时间没有使用这种方法了,因为我通常会让我的db全局
跳过对每个文件执行此操作(请参见此处的示例:)没有帮助,我也尝试删除。连接
,但仍然没有成功。我会尝试看看你的链接,谢谢。