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

为什么在承诺给我一个错误之后在ExpressJS中使用then()回调?

运维笔记admin11浏览0评论

为什么在承诺给我一个错误之后在ExpressJS中使用then()回调?

为什么在承诺给我一个错误之后在ExpressJS中使用then()回调?

我是NodeJS和ExpressJS的新手。我正在尝试执行一个简单的数据库查询。

db.execute('SELECT * FROM restaurant').then().catch()

我收到这个错误:

db.execute('SELECT * FROM restaurant')。then()。catch()^

TypeError:无法读取未定义的属性'then'

我正在使用phpMyAdmin上运行的MySQL数据库。这是我的数据库快照。

我检查了我的数据库,它已连接。下面是我在App.js中的代码!

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const admin = require('./routes/admin');
const shop = require('./routes/shop');
const db = require('./config/db');

const app = express();
app.use(bodyParser.urlencoded({extended:false}))

app.use('/admin',admin);
app.use('/shop',shop)

db.execute('SELECT * FROM restaurant').then().catch()

app.get('/', (req,res) =>{
res.status(200).send('<h3>Front page</h3>')
});


app.get('*', (req,res) =>{
res.status(404).send('<h1>File not found</h1>')
});

const port = process.env.PORT || 5000;
app.listen(port);

console.log('App is listening on port ' + port);

我想要在浏览器上显示餐厅的名称。

下面是config / db文件:

var mysql = require('mysql2');
var db = mysql.createPool({
host : 'localhost',
user: 'root',
database:'pos',
password:''
});

module.exports=db 
回答如下:

您需要在配置文件中导出池的promisified版本,如下所示:

var mysql = require('mysql2');
var db = mysql.createPool({
   host : 'localhost',
   user: 'root',
   database:'pos',
   password:''
});

module.exports=db.promise()

有关更多信息,请查看mysql2 docs。

发布评论

评论列表(0)

  1. 暂无评论