[solved] TypeError: this.userId

Hi Guys, I am trying to return on one user by using:

ecrtr: function(){
var a = Meteor.users.findOne({_id: this.userId()}, {fields: {“recruiterId”: 1} });
return a;

A Result i am unable to return the user

TypeError: this.userId is not a function

Please can Someone Kindly help me with this. Please!

Thank You So Much

call in publish or method function:

Thank you so much for your quick reply @minhna.

Basically, I did call it from a p/s method.
It still doesn’t work

define your methods like this.

  ecrtr() {
    var a = Meteor.users.findOne({_id: this.userId()}, {fields: {“recruiterId”: 1} });
    return a;
1 Like

Just to clarify: In methods and publications, this.userId is not a function:

Meteor.users.findOne({_id: this.userId}, {fields: {“recruiterId”: 1} });

Thank you so much @minhna. I will try to update my code asap and get back you :heart:

Still not working :frowning:

try this.userId instead of this.userId() :joy:

1 Like

Still not working. Only Meteor.userId() works but i only need for the current user. :frowning:

I usually use
const me = Meteor.user();

Yeah, i tried this one too but it return all users id

Can you show more code? As far as I can see from your previously posted code, this is likely a scoping issue, but I can’t tell for sure without more code to provide a proper context.

1 Like

Thank You So Much Guys. @minhna @robfallows @copleykj. I finally made it works by using @minhna last proposed solution.


Something weird I also got this.userId worked in another component.

I’d definitely try to stick with this.userId and try to figure out the issue causing this since this will work in a method, but it won’t inside a publication, and you’ll want to understand the error that you’ve made so that you won’t make the same one in the future.

If you are willing to give more detailed code, or you have repo that I can clone and look at, then I’d be more than willing to help you figure out what the issue is… As I stated previously I believe this is probably just a scoping issue, but I won’t know for sure without more context.


Hi @copleykj,
inside a publication do you mean it like this? https://galaxy-guide.meteor.com/apm-improve-cpu-and-network-usage.html#Always-Do-a-Null-Check-for-id
I think this.userId will not work on a server side method (method called by the server, on the server) or pretty much anywhere on the client.

Server only method - no access to a user Id via ‘this’.
Server defined, client called method: use ‘this.userId’ within the method.
Client - use Meteor.userId()

@morningstardev13 the function as you describe can use this.userId (not this.userId()) if you include it in a method accessible both on the server and client.
Let’s say you have a file called ‘methods.js’. You write your method, and import this file on both server and client startup.


import { Meteor } from 'meteor/meteor'
import { ValidatedMethod } from 'meteor/mdg:validated-method'
import SimpleSchema from 'simpl-schema'

export const yourMethodName = new ValidatedMethod({
  name: 'yourMethodName',
  validate: new SimpleSchema({
    some value to validate if any: String, ...
  run ({ values if any }) {
    if (Meteor.isServer) {
      if (!this.userId) { throw new Meteor.Error('not-authorized') } // you are on the server here
    return Meteor.users.findOne({ _id: this.userId }, { fields: { “recruiterId”: 1 } })
   // or return Meteor.users.find({ _id: this.userId }, { recruiterId: { $exists: true } } }).count() (add limit: 1 just in case but on the _id should not be necessary). This is when you want to return count 1 if exists or 0 if it doesn't exists if you are forced to use a boolean as a return of the method.

On client:

Meteor.call(‘yourMethodName’, { values if any or empty }, (err, res) => {…handle both err and res })

So this was for ‘this.userId’
Otherwise, at any point on the client you can use:

let a = Meteor.users.findOne({_id: Meteor.userId()}, {fields: {“recruiterId”: 1} }); // get an object { _id: ..., recruiterId: .... }
// but Meteor.users.findOne({_id: Meteor.userId()}) = Meteor.user() as long as you have a publication for the necessary fields

let a = Meteor.user().recruitertId // get a string or number or undefined
// or with a guard if no user is logged in.
const user = Meteor.user()
let a = user && user.recruiterId . // or Meteor.user() && Meteor.user().recruiterId
1 Like

this.userId is available inside method and publications by design. They are there for a reason. You are right the aren’t available in most code on the client except a method simulation. The reason for this is that these are the only places on the server that you have access to the connection context which holds the information about the currently connected user. You should never have an issue with getting this.userId within the context of a publication or method invocation, unless the user isn’t signed in.

On the topic of server to server method calls… Unless you have a separate DDP connection to another server, there is absolutely no reason for this. If you find yourself doing this you need to properly refactor your app so that the code is moved into its proper domain and then call that code from within a method if and only if the method is invoked from the client, otherwise just call the function.



Did you end up figuring this out? If not, I may have a possible explanation.

Is the userId in question different to the currently logged in userId? If so, then the method call will have a different outcome on the client and the server.

The client will return undefined (unless it has a publication granting it access to the specific user document in question). To fix this, wrap that part of your method logic in an:

if (Meteor.isServer())

And then return the data you need to the client (handling it via the method callback). That should fix it for you.

(Let me know if the above doesn’t make sense)

1 Like

Thank you so much @hemalr87. I think this one should work too. Also as @copleykj said, it was a scoping problem. I fixed it. Thank you so much.

@minhna solution was fast and Amazing :heart_eyes:.