Mdg_validated-method.js?hash=fb1c067…:119 Uncaught TypeError: Accounts.findUserByEmail is not a function(…)

Uncaught TypeError: Accounts.findUserByEmail is not a function(…) mdg_validated-method.js?hash=fb1c067…:119

import {
  Accounts
} from 'meteor/accounts-base';

export const createschoolManager = new ValidatedMethod({
  name: 'users.create.schoolManager',
  validate: null,
  run(newUser) {
    console.log(Accounts);
    const existUser = Accounts.findUserByEmail(newUser.email);
    if (existUser) {
      return {
        "error": "User already uses this email to register"
      }
    }
    return Accounts.createUser({
      email: newUser.email,
      password: newUser.password,
      profile: {
        "profile.name": newUser.name,
        "profile.address": newUser.address,
      }
    });
  }
});

I suspect the issue here is that the validated method’s run method is executed on the client and the server. However, Accounts.findUserByEmail is only available on the server. You could do something like:

...
run(newUser) {
  if (Meteor.isServer) {
    console.log(Accounts);
    const existUser = Accounts.findUserByEmail(newUser.email);
    if (existUser) {
      return {
        "error": "User already uses this email to register"
      }
    }
  }
...
1 Like