我不确定调用了什么样的nodejs,所以请原谅我,如果你知道确切的话,请告诉我。
所以提出我的问题,我精通nodejs并在之前的代码中使用异步等待很多。但现在我需要将我的代码迁移到oop编码风格(可能是打字稿样式?)。所以我真的不确定如何使用async await。这是我到现在为止所尝试的:
== server.js
import bookController from './routes/books/bookController';
import bookService from './routes/books/bookService';
import bookQueries from './routes/books/bookQueries';
let bookCtrl = new bookController(bookService, bookQueries);
app.get('/books/allbooks', (request, response) => {
bookCtrl.getAllBooks(request, response);
});
== bookController.js
export default class bookController extends baseController {
constructor(bookService, bookQueries){
super();
this.bookService = new bookService();
this.bookQueries = new bookQueries();
}
async getAllBooks(request, response){ // Using async here throws error
var conn = await this.getBookConn();
var result = await conn.query("select * from books");
response.status(200).json(result);
}
}
==基本控制器 **
import mysql from 'promise-mysql';
export default class baseController {
constructor(){
}
getBookConn(){
let bzConn = mysql.createConnection({
host: 'abc.com',
database: 'test',
user: 'test',
password: 'test',
insecureAuth: true
})
return bzConn;
}
}
**