Nate Barr
Nate Barr

Reputation: 4660

Meteor.users collection is undefined on server

What's wrong with this code? It's inside of a "server" folder. Line 11 here, is called out in the error log.

import { Accounts } from 'meteor/accounts-base';
import generatePincode from '../../../utils/generate-pincode';
import Meteor from 'meteor/meteor';

Accounts.onCreateUser((options, user) => {
  const customizedUser = Object.assign({
    'pincode': generatePincode(4),
  }, user);

  // check that the pincode doesn't already exist
  const existingUser = Meteor.users.findOne({
    'pincode': customizedUser.pincode,
  });
  if (existingUser) {
    throw new Meteor.Error(500,
      'Duplicate pincode generated, please try again.');
  }

  // We still want the default hook's 'profile' behavior.
  if (options.profile) {
    customizedUser.profile = options.profile;
  }
  return customizedUser;
});

The terminal (server) logs:

Exception while invoking method 'createUser' TypeError: Cannot read property 'findOne' of undefined

Upvotes: 0

Views: 298

Answers (1)

coagmano
coagmano

Reputation: 5671

Your import Meteor line is incorrect, which means you're working with the default or namespace export. In the case of Meteor, that means you get an object with:

{
  Meteor: [Meteor object],
  global: [Window on client and global on server],
  meteorEnv: [env vars]
}

Try this to get Meteor directly:

import { Meteor } from 'meteor/meteor';

That should fix it.

If it doesn't, try meteor remove accounts-password and meteor add accounts-password again

Upvotes: 2

Related Questions