Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Can this be used in api calls? #11

Open
JaiyashreeSM opened this issue Dec 18, 2018 · 4 comments
Open

Can this be used in api calls? #11

JaiyashreeSM opened this issue Dec 18, 2018 · 4 comments

Comments

@JaiyashreeSM
Copy link

JaiyashreeSM commented Dec 18, 2018

Please carify if this can be used in node APIs where the methods from different files are being called in a single api and should carry some dynamic value throughout the files to be used in all methods.

@ghost
Copy link

ghost commented Dec 19, 2018

Of course, I'm using it right now to track the request ID propagation throughout the app.

@vicanso
Copy link
Owner

vicanso commented Dec 20, 2018

@JaiyashreeSM If you don't know how to use async-local-storage, you can show me your codes and I may give you some suggestions.

@JaiyashreeSM
Copy link
Author

JaiyashreeSM commented Dec 20, 2018

@vicanso, We use loopback and here is the simple flow. I get the value being set in the incoming request here in server.js but not carried to test.js
The call from rest-client first comes here...
server.js
`const als = require('async-local-storage');

app.all('*', function(req, res, next) {
console.log(Request for --- ${req.method} : ${req.originalUrl});
als.set('reqTenant', req.headers.tenant || 'someTestValue');
next();
});`
the control then comes to the file where the api-method is executed

test.js

`const als = require('async-local-storage');
Test.remoteMethod('testAPI', {
description: 'Test api',
accepts: [],
http: {path: '/test', verb: 'get'},
returns: {arg: 'msg', type: 'object', root: true}
});

Test.testAPI = (options, callback) => {
const searchText = als.get('reqTenant');
Test.find({
where: { name: {ilike: searchText } },
}, (err, res) => {
callback(err, res)
});
};`

@vicanso
Copy link
Owner

vicanso commented Dec 24, 2018

@JaiyashreeSM

I am sorry for late replying. You should call the enable function like the example.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants