最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

无法从另一个文件进行查询(connection.query不是函数)

运维笔记admin8浏览0评论

无法从另一个文件进行查询(connection.query不是函数)

无法从另一个文件进行查询(connection.query不是函数)

im是node.js和数据库中的新增功能。尝试执行mysql查询,但出现此错误TypeError: connection.query is not a function

我正在使用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

感谢您的帮助。

回答如下:

bookRoutes.js

const mysql = require('mysql2');
const debug = require('debug')('app:bookRoutes');

const connection = require('../../app').connection; // identify the exported module

// the rest…
发布评论

评论列表(0)

  1. 暂无评论