Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
express http上下文在调用mysql后丢失_Mysql_Node.js_Express - Fatal编程技术网

express http上下文在调用mysql后丢失

express http上下文在调用mysql后丢失,mysql,node.js,express,Mysql,Node.js,Express,我需要你的帮助。我正在尝试为我的nodejs应用程序实现全局请求id。为此,我使用ExpressHTTP上下文库来设置请求id。但当我的代码从mysql数据库访问数据时,它并没有像预期的那样工作。似乎express http context和mysql库使用的cls中存在兼容性问题 下面提供了示例代码和输出: const-app=require('express')(); const httpContext=require('express-http-context'); const mysq

我需要你的帮助。我正在尝试为我的nodejs应用程序实现全局请求id。为此,我使用ExpressHTTP上下文库来设置请求id。但当我的代码从mysql数据库访问数据时,它并没有像预期的那样工作。似乎express http contextmysql库使用的cls中存在兼容性问题

下面提供了示例代码和输出:

const-app=require('express')();
const httpContext=require('express-http-context');
const mysql=require('mysql');
const{INTERNAL_SERVER_ERROR,OK,NOT_FOUND}=require('http-status-code');
const http=require('http');
const uuidv4=require('uuid/v4');
constpool=mysql.createPool({
主持人:“127.0.0.1”,
港口:3306,
用户:'根',
密码:'Redhat@123',
数据库:“用户”,
连接限制:10,
});
pool.query('选择1+1作为解决方案',(错误)=>{
如果(错误){
log('无法连接到MySQL:-',错误消息);
过程。退出(1);
}
log('MySQL PING正在工作');
});
app.use(httpContext.middleware);
应用程序使用((请求、恢复、下一步)=>{
set('requestId',uuidv4());
log('request Id set为:',httpContext.get('requestId');
next();
});
const getUserDetailsRepository=(用户ID,回调)=>{
console.log('inside-getuserDetails repository for request id:'),httpContext.get('requestId');
pool.getConnection((connectionError,connection)=>{
如果(连接错误){
console.log('get ConnError getuserDetails repository for request id:',httpContext.get('requestId');
回调(connectionError);
}否则{
常量查询='SELECT*from user,其中id=?';
query(查询,[userId],(queryError,results)=>{
连接。释放();
如果(查询错误){
log('get queryError getuserDetails repository for request id:'、httpContext.get('requestId')、queryError.message);
回调(查询错误);
}否则{
console.log('getuserDetails存储库中获取请求id的响应:',httpContext.get('requestId');
回调(空,结果);
}
});
}
});
};
const userGetDetails=(请求、恢复、下一步)=>{
const{userId}=req.params;
console.log('inside-getUserDetails controller for request id:',httpContext.get('requestId');
getUserDetailsRepository(用户ID,(错误,结果)=>{
如果(错误){
console.log('getuserDetails控制器中请求id为“”时出现错误,httpContext.get('requestId'))
res.sendStatus(内部服务器错误);
}否则,如果(结果){
console.log('getuserDetails存储库中获取请求id的响应:',httpContext.get('requestId');
res.status(OK).json(结果);
}否则{
res.sendStatus(未找到);
}
});
};
app.get('/user/:userId',userGetDetails);
const server=http.createServer(app);

server.listen(3000,()=>console.log('Http服务器在端口上开始侦听')我希望此解决方案能解决您的问题,并为有此问题的人节省许多时间

您可以使用bindEmitter来解决这个问题

app.use((req, res, next) => {
  httpContext.ns.bindEmitter(req);
  httpContext.ns.bindEmitter(res);
  var requestId = req.headers["x-request-id"] || uuidv4();
  httpContext.set("requestId", requestId);
  console.log('request Id set is: ', httpContext.get('requestId'));
  next();
});

我希望这个解决方案能解决您的问题,并为有此问题的人节省很多时间

您可以使用bindEmitter来解决这个问题

app.use((req, res, next) => {
  httpContext.ns.bindEmitter(req);
  httpContext.ns.bindEmitter(res);
  var requestId = req.headers["x-request-id"] || uuidv4();
  httpContext.set("requestId", requestId);
  console.log('request Id set is: ', httpContext.get('requestId'));
  next();
});