mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-31 01:28:30 +08:00
async await
This commit is contained in:
@ -18,16 +18,16 @@ const app = express();
|
|||||||
|
|
||||||
app.use(rTracer.expressMiddleware());
|
app.use(rTracer.expressMiddleware());
|
||||||
|
|
||||||
app.get('/getUserData/{id}', (req, res, next) => {
|
app.get('/getUserData/{id}', async (req, res, next) => {
|
||||||
usersRepo.find(req.params.id)
|
try {
|
||||||
.then((user) => {
|
const user = async usersRepo.find(req.params.id);
|
||||||
|
|
||||||
// At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable
|
// At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable
|
||||||
const transactionId = rTracer.id();
|
const transactionId = rTracer.id();
|
||||||
logger.info(`user ${user.id} data was fetched successfully`, { transactionId });
|
logger.info(`user ${user.id} data was fetched successfully`, { transactionId });
|
||||||
|
|
||||||
res.json(user);
|
res.json(user);
|
||||||
})
|
} catch (err) {
|
||||||
.catch(err) {
|
|
||||||
// As next step I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code
|
// As next step I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code
|
||||||
logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err });
|
logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err });
|
||||||
next(err);
|
next(err);
|
||||||
@ -48,11 +48,6 @@ app.use(rTracer.expressMiddleware({
|
|||||||
headerName: 'x-transaction-id'
|
headerName: 'x-transaction-id'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// The supplied TransactionId will be now the one that was passed from the previous request; if this is the first request in the chain, it will be just generated
|
|
||||||
const transactionId = rTracer.id();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user