I have a Meteor application with the following fixtures code:
/imports/startup/server/fixtures.js
import { Meteor } from 'meteor/meteor'
import { Accounts } from 'meteor/accounts-base'
if(Meteor.users.find().count === 0) {
const users = [
{username: "admin", email: "[email protected]", profile: { name: "Admin" }, roles: ["admin"]},
{username: "school", email: "[email protected]", profile: { name: "School Name" }, roles: ["school"]},
{username: "teacher", email: "[email protected]", profile: { name: "Teacher" }, roles:["teacher"]}
]
for(let user of users) {
Accounts.createUser({
username: user.username,
email: user.email,
password: "123456",
profile: {
name: user.profile.name,
},
roles: user.roles
})
}
}
On starting up my project all the accounts are created successfully except none of them have the roles field. What am I doing wrong?
What you are doing wrong is that you pass options into the function that are not accepted by it. createUser options
only accepts username
, email
, password
and profile
. You should study the docs, meteors API is very well documented.
Now, to set the user roles you have a couple of options, one of them would be use the _id
of the newly created user, which is returned by createUser
and then set the roles like so:
const userId = Accounts.createUser({
username: user.username,
email: user.email,
password: "123456",
profile: {
name: user.profile.name,
});
Roles.addUsersToRoles(userId, user.roles)
assuming that this is server side code. On the client this won't work. You could also set the roles directly using a Meteor.users.update();
call or fiddle around with Accounts.onCreateUser
callback, which is very nice for manipulating everything that you pass into createUser
. Hope that helps.