This may be a bit of a basic question, but in some...
# support-questions
c
This may be a bit of a basic question, but in some cases the supertokens instance is not found even though I initialize Supertokens as the first thing in the app. Now I am trying to get the user count in a request handler, and it is saying that Supertokens is not initialized :/
Copy code
await supertokens.getUserCount()
so... Do I have to do anything special to get the instance?
r
well, if you are seeing the error, it means that the code for initialising supertokens has not been called
you may think that it's being called, but it's not
cause otherwise the error would not come
c
But it is called as the first thing in the app 😄
I think the Sails framework might be in the way here
this is a request handler in a Sails controller
r
put a console log before calling the init function and before you call the getUserCount function and see which one is printed first
c
yeah I have, the init is printed first
r
can you upload the code to github in a way that i can replicate it?
c
Sails might require the controllers a bit weirdly, and I'm not fully sure what's going on. But the instance should still be there as it's not a new thread or anything
I'll try
r
well.. im not entierly sure how sails works
c
I don't like sails
but it's a legacy app so...
I am constantly evaluating the time cost of dumping it while working on this lol 😄
r
yea well.. it can be difficult to use a framework that you are not fully familiar with
but maybe ask on their community about this and how it affects singletons
c
hmm yeah good idea
r
in the worst case, you can just call the init function before calling getUserCount
c
It uses this to require the controller modules: https://www.npmjs.com/package/include-all
r
and then it should work
c
Okay I'll try that
Putting it in a different module and importing it in the controller works 🤷‍♂️
I still had a require of supertokens in the controller file, and it didn't start working until I removed it. You could look into not loading the instance before something from the supertokens import is used
Without peeking at the code, it seems that the Supertokens instance is loaded immediately when importing the module, and then when I try to use a method it throws the error because it doesn't have the instance even if the instance WOULD be available when calling the method
r
Not sure i completely follow your reasoning here.
but if you call supertokens.init, it creates a singleton in memory. As long as the memory is shared, future calls to supertokens' functions would not throw that error
c
So I had the import
import supertokens from 'supertokens-node';
in the controller file, and presumably the controller file was loaded somehow before supertokens.init() was called. At this point, Supertokens seems to already try to "load" (or whatever) the initialized instance. If it doesn't exist, nothing is thrown. Then when I tried to use a method (example
supertokens.getUserCount()
) in a request handler, it throws the "init not called" error. It would be better if the instance was loaded when I call the getUserCount method instead of immediately upon importing the module
r
to load the instance, it requires you to call the init function first
i mean creating a singleton only happens when the init function is called
creating an instance when getUserCount is called is not possible since we don't have the info needed to actually create an instance
c
Okay I'll look at the code at some point
this was just a theory 😄 But I didn't mean creating a new instance when getUserCount is called
r
ah right. Then i misunderstood.
c
Yeah, I should look at the code before theorising 😄
but the thing works in my app now so all is good
2 Views