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
setUsername
uses the checkForCaseInsensitiveDuplicates
method which throws a 403 error in the case of a duplicate:
if (fieldValue && !skipCheck) {
var matchedUsers = Meteor.users.find(
selectorForFastCaseInsensitiveLookup(fieldName, fieldValue)).fetch();
if (matchedUsers.length > 0 &&
// If we don't have a userId yet, any match we find is a duplicate
(!ownUserId ||
// Otherwise, check to see if there are multiple matches or a match
// that is not us
(matchedUsers.length > 1 || matchedUsers[0]._id !== ownUserId))) {
throw new Meteor.Error(403, displayName + " already exists.");
}
}
};
// XXX maybe this belongs in the check package
var NonEmptyString = Match.Where(function (x) {
check(x, String);
return x.length > 0;
});
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…
1 Like
Haha - no probs. You’re most welcome