I’m new to Meteor and trying to figure out, how Account.createUser() works when the user is in the database already and meteor checks for duplicate entries.
I’ve created a Meteor method:
'test.createUser'(user) {
try {
Accounts.createUser(
{
username: user.username,
email: user.emails[0].address,
profile: user.profile,
},
);
} catch (exception) {
console.log('Error creating user:', exception);
throw exception;
}
},
Which I call on the client side:
createUser() {
Meteor.call(
"test.createUser",
{
username: "myuser",
emails: [{ address: "myuser@example.com", verified: true }],
profile: {
profile: "userProfile",
name: "New User",
path: null,
},
},
(error) => {
if (error) {
console.error("Error:", error);
}
}
);
},
If the user did not exist in mongo and test.createUser is called the 1st time, the user is created. On subsequent calls, I’d expect that the catch-block of test.createUser runs, as the username and email are in use already.
However, this does not happen. Instead, Meteor crashes with the error displayed on the console:
W20241231-09:01:11.046(1)? (STDERR) meteor://app/packages/accounts-base.js:685
W20241231-09:01:11.046(1)? (STDERR) const error = new Meteor.Error(errorCode, isErrorAmbiguous ? ‘Something went wrong. Please check your credentials.’ : msg);
W20241231-09:01:11.046(1)? (STDERR) ^
W20241231-09:01:11.067(1)? (STDERR) errorClass [Error]: Something went wrong. Please check your credentials. [403]
W20241231-09:01:11.067(1)? (STDERR) at AccountsServer._handleError (packages/accounts-base/accounts_server.js:1523:19)
W20241231-09:01:11.068(1)? (STDERR) at AccountsServer._checkForCaseInsensitiveDuplicates (packages/accounts-base/accounts_server.js:1491:14)
W20241231-09:01:11.068(1)? (STDERR) at processTicksAndRejections (node:internal/process/task_queues:105:5)
W20241231-09:01:11.068(1)? (STDERR) at AccountsServer._createUserCheckingDuplicates (packages/accounts-base/accounts_server.js:1504:5)
W20241231-09:01:11.068(1)? (STDERR) at AccountsServer.createUser (packages/accounts-password/password_server.js:996:12) {
W20241231-09:01:11.068(1)? (STDERR) isClientSafe: true,
W20241231-09:01:11.068(1)? (STDERR) error: 403,
W20241231-09:01:11.068(1)? (STDERR) reason: ‘Something went wrong. Please check your credentials.’,
W20241231-09:01:11.068(1)? (STDERR) details: undefined,
W20241231-09:01:11.068(1)? (STDERR) errorType: ‘Meteor.Error’
W20241231-09:01:11.068(1)? (STDERR) }
W20241231-09:01:11.068(1)? (STDERR)
W20241231-09:01:11.068(1)? (STDERR) Node.js v22.11.0
Catching the exception and re-throw it does not work.
I’ve also tried to call Accunts.createUser on the client side:
createUser2() {
const user = {
username: "myuser",
emails: [{ address: "myuser@example.com", verified: true }],
password: "secret",
profile: {
profile: "userProfile",
name: "New User",
path: null,
},
},
};
try {
Accounts.createUser(
{
username: user.username,
email: user.emails[0].address,
password: user.password,
profile: user.profile,
},
(err) => {
console.log("we got an error:", err);
}
);
} catch (exception) {
console.log("Error creating user:", exception);
throw exception;
}
},
},
we got an error:
{
“isClientSafe”: true,
“error”: 403,
“reason”: “Something went wrong. Please check your credentials.”,
“message”: “Something went wrong. Please check your credentials. [403]”,
“errorType”: “Meteor.Error”
}
In client mode, the callback containing the error runs.
But how can I catch it when using the Meteor method and avoid crashing Meteor?