【发布时间】:2021-10-06 07:43:29
【问题描述】:
我尝试在异步存储 onRequest 中添加跟踪 ID。但是在处理程序中,没有存储数据。为什么处理程序中的数据为空?我做错了什么?
异步本地存储:
const {AsyncLocalStorage} = require('async_hooks')
const asyncLocalStorage = new AsyncLocalStorage()
module.exports = {
asyncLocalStorage
}
设置:
server.ext({
type: 'onRequest',
method(request, h) {
return new Promise(resolve => {
getLocalStorage().run({meta: {traceId: 'id'}}, () => {
return resolve(h.continue)
})
})
},
})
处理程序:
{
path: '/',
method: 'GET',
handler: (req) => {
const data = asyncLocalStorage.getStore()
//why data is undefined?
return data;
},
options: {
tags: ['api', 'Test'],
description: "Test",
auth: {
strategy: 'jwt_user',
scope: ['USER'],
},
response: {
schema: testResponseSchema,
modify: true,
options: {
stripUnknown: true,
convert: true,
},
},
},
},
【问题讨论】:
-
好问题,我正在努力实现同样的目标,但出于某种原因,它只是在表达上有效,而使用 HAPI 似乎是个大问题。
标签: node.js hapijs asyncstorage hapi