MySQL отлично работает с простыми обратными вызовами, но я хочу выполнять несколько запросов mysql асинхронно в express node.js, для этого я использую asyn c await с обещанием. Я также пробовал использовать обещание.allSettled, но он по-прежнему дает ту же ошибку. Вот полный код.
dbConn. js
var config = require('../config');
var conn = (module.exports = require("mysql").createConnection({
host: config.dbHost,
user: config.dbUser,
port: config.dbPort,
password: config.dbPassword,
database: config.dbName,
}));
conn.connect(err => {
if (err) {
return console.log(err.stack);
} else {
return console.log("Connected!");
}
});
Route File.
var conn = require('../DB/dbConn');
router.get('/search',async function(req, res, next) {
try {
const CourseContent = await conn.query( "SELECT cd.*,s.*,u.* from coursesdetails cd,subjects s,universities u where cd.subjectId = s.subjectId and u.universityId = cd.universityId and s.subjectNameEn like '%Art%';" );
const Allsubjects = await conn.query("select * from subjects;");
const AllCountries = await conn.query("SELECT distinct Country from worldcities;");
const AllDisciplines = await conn.query("select * from discipline;");
const promises = [CourseContent,Allsubjects,AllCountries,AllDisciplines];
Promise.all(promises).then((results) => {
console.log(results);
res.render('search', { results: results,layout: 'layouts/index-layout' });
}).catch((error)=> {console.log(error);})
} catch (error) {
console.error(error);
}
});
ответ от Promise.all результаты
[
Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\myste\Documents\Office Work\FindCoursesNode.js\routes\course.js:58:38
at Layer.handle [as handle_request] (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\express\lib\router\index.js:275:10),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: "SELECT cd.*,s.*,u.* from coursesdetails cd,subjects s,universities u where cd.subjectId = s.subjectId and u.universityId = cd.universityId and s.subjectNameEn like '%Art%';",
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 25,
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
},
Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\myste\Documents\Office Work\FindCoursesNode.js\routes\course.js:59:36
at processTicksAndRejections (internal/process/task_queues.js:97:5),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'select * from subjects;',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 25,
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
},
Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\myste\Documents\Office Work\FindCoursesNode.js\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\myste\Documents\Office Work\FindCoursesNode.js\routes\course.js:60:37
at processTicksAndRejections (internal/process/task_queues.js:97:5),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'SELECT distinct Country from worldcities;',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 25,
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
}...
Node.js версия 12.18.1