Fail behaviour of Accounts.setUsername

Hi there,

i’m experiencing problems with the Accounts.setUsername function. Whenever i try to set the username of a user and the username is not already in use it works just fine. But when it is already in use the method just aborts. No error is thrown and the script just stops.

A short example

const username = "chris";
const id = "aaa12345";
Accounts.setUsername(id, username);
console.log("done");

when “chris” is not already in use it will be set correctly and then print “done”.
when “chris” is already in use it will do nothing and “done” is not printed.

The docs say the function will fail in the case of an existing username but how am i supposed to know whether it did? http://docs.meteor.com/api/passwords.html#Accounts-setUsername

Am i overlooking something?

meteor is at 1.4.2.3
accounts-base is at 1.2.14

any help would be greatly appreciated :slight_smile:

setUsername uses the checkForCaseInsensitiveDuplicates method which throws a 403 error in the case of a duplicate:

So I suggest:

const username = "chris";
const id = "aaa12345";
try {
  Accounts.setUsername(id, username);
  console.log("done");
} catch(err) {
  console.log('Error', err.reason);
}
1 Like

thanks! i just shouldn’t code on mondays… :hushed:

1 Like

Haha - no probs. You’re most welcome :slight_smile: