Search code examples
meteormeteor-accounts

Meteor accounts password: TypeError: Accounts.setPassword is not a function


I am using: Meteor Version 1.8, [email protected]

When invoking:

  Meteor.methods({
    setPassword(newPassword, userId) {
      check(userId, String);
      check(newPassword, String);
      if(Meteor.user().isAdmin){
        Accounts.setPassword(userId, newPassword);
      }
    },
  });

by

Meteor.call('setPassword', password, this.userId);

i get this error: Exception while simulating the effect of invoking 'setPassword' TypeError: Accounts.setPassword is not a function but the password is still set...


Solution

  • Meteor methods can run on both server and client side (see here). Here the error is coming from the client side : simulating the effect means the client is trying to compute an optimistic answer to your query to the server.

    The Accounts object is available both client and server side, but I bet that the Accounts.setPassword function is only available in the server for security reasons.

    To avoid the error, you can either : Place the meteor method definition in a server-only folder see here (like in this file app_code/imports/api/accounts/server/methods.js), or wrap it with if(Meteor.isServer) see here as such:

    if(Meteor.isServer){
        Meteor.methods({
            setPassword(newPassword, userId) {
                check(userId, String);
                check(newPassword, String);
                if(Meteor.user().isAdmin){
                    Accounts.setPassword(userId, newPassword);
                }
            },
        });
    }