Meteor.userId can only be invoked in method calls or publications

This is a function file and this is what I have and I’m getting Meteor.userId can only be invoked in method calls or publications error.

   getCurrentUserId() {
    let userId = Meteor.userId();
   console.log(userId);
    return userId;
  }

this userId display id on my console but I didn’t see any result on my terminal (I see the error message though)

I wrapped the block with Meteor.isClient and this help me deal with the error message above but break other part of the app.

   getCurrentUserId() {
    let userId;
    if (Meteor.isClient) {
      userId = Meteor.userId();
    }
   console.log(userId);
   return userId;
  }

I console log userId and see output on the console but get undefined on my terminal.

My thought is that the app needs userId to be available both for client and server side and is there a way for it?

The question is where are you calling this from?

AuserID is associated with a connection - if you’re trying to find out the userID in a startup block, at init time, or in a defer, timeout or interval (amongst others depending on things like REST APIs, cron jobs, etc) it wont work.

I can call this anywhere. Meteor.functions.getCurrentUserId will return current user’s id.
I have a few calling this inside meteor methods and I also use react.js so inside of the components.

That’s what you are asking?

You’re saying you get an error, I’m asking where is the root location you call this from that you get an error. Because, exactly as the error says, you can only call this on the server inside a method or publication.

Are you getting an error when you call that from a method or publication, or are you trying to call it from somewhere else (like the terminal) where it would have no way of knowing what the user id is and therefore correctly throws an error.