mysql - 某些路由在 ExpressJS 中不起作用
问题描述
我正在开发一个快速 API,但我的 R_Consultations.js 文件有问题
const express = require('express');
var mysql = require('mysql');
const router = express.Router();
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "cline_db"
});
router.get('/',function findAllConsultationsOfOnePatient(req, res) {
//var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
// Executing the MySQL query (select all data from the 'users' table).
con.query('SELECT id_consultation,date_consultation, motif,Consultation.id_patient,Consultation.id_medecin FROM Consultation ', function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
}
);
router.get('/:cin',function findAllConsultationsOfOnePatient(req, res) {
//var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
// Executing the MySQL query (select all data from the 'users' table).
con.query('SELECT id_consultation,date_consultation, motif,Consultation.id_patient,Consultation.id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=?',[req.params.cin], function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
}
);
router.get('/statistiques_annee',function getStatistiquesOfCetteAnnee(req, res) {
// Executing the MySQL query (select all data from the 'users' table).
con.query("SELECT count(id_consultation) as nombre_consultations FROM Consultation", function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
}
);
//le nombre Total des consultaions d'un patient:
router.get('/statistiques/:cin',function findNumberOfAllConsultationsOfOnePatient(req, res) {
//var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
// Executing the MySQL query (select all data from the 'users' table).
con.query('SELECT count(Consultation.id_consultation) as nombre_consultations FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=?',[req.params.cin], function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
}
);
router.get('/statistiques_mois',function getStatistiquesOfCeMois(req, res) {
//var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
// Executing the MySQL query (select all data from the 'users' table).
con.query('SELECT count(id_consultation) as nombre_consultations FROM Consultation where Month(date_consultation)=Month(curdate())', function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
console.log(results)
});
}
);
module.exports = router;
当我运行“节点服务器”时
并调用“ http://localhost:5000/d_consultations/statistiques_mois ”,我有空反馈“[]”。
我所有的路线都有效,除了这条路线不起作用??
当我在文件“server.js”中对 con.use() 使用相同的 sql 请求时,它可以 100% 工作。
有什么问题 ?
解决方案
推荐阅读
- php - AWS PHP SDK 仅适用于 Cognito 服务
- r - 如何在 R Shiny 应用程序中显示整个数据表?
- mysql - 为什么要在数据库中存储文件名和上传路径?
- python - 用 BeautifulSoup 解析 td 元素
- java - 方法检查给定输入文件的值 [java]
- javascript - 如何在 Thymeleaf 和 Spring Boot 中进行复杂的表单设计(ZK 经验丰富的用户)
- vba - 如何将类名作为参数传递?
- wordpress - 如何将新按钮添加到古腾堡编辑工具栏
- java - RestFB如何订阅微博
- objective-c - 多个调度组导致崩溃