简体   繁体   中英

Roles with Meteor and React

I am currently working on a project for a client. I'm trying to assign roles to users at the time they sign up for an account.

handleSubmit(e){
    e.preventDefault();
    this.setState({message:'',messageClass:'hidden'});
    var that = this;
    var first_name = ReactDOM.findDOMNode(this.refs.first_name).value.trim();
    var last_name = ReactDOM.findDOMNode(this.refs.last_name).value.trim();
    var email = ReactDOM.findDOMNode(this.refs.email).value.trim();
    var password = ReactDOM.findDOMNode(this.refs.password).value.trim();
    var type = 'fan';
    var user = {email:email,password:password,profile:{fullname:(first_name + last_name).toLowerCase(),firsname:first_name,lastname:last_name,avatar:'http://placehold.it/150x150',friends:[],type:type}};
    Accounts.createUser(user,function(e){
        if (e) {
            Materialize.toast(e.reason, 5000);
        } else {
            FlowRouter.go('/dashboard');
        }
    })
}

Above is my handle submit event for the signup form. Below is the hook that I added to assign the role to the user after the user is inserted.

Meteor.users.after.insert(function (userId, doc) {
if (doc.profile.type === "fan") {
    Roles.addUsersToRoles(doc._id, [ROLES.Fan])
}

});

What I'm finding is that the user gets created but instead of being redirected to the dashboard, the user remains on the signup page and receives an error in the toaster message stating internal server error. When I go into the console to find the user that was created, the profile type is assigned but the user is missing their email address.

This is the error I'm getting in my terminal window:

Exception while invoking method 'createUser' ReferenceError: ROLES is not defined I20160402-16:04:42.482(-4)? at Object. (both/collection_hooks/hooks.jsx:3:35) I20160402-16:04:42.482(-4)?
at packages/matb33_collection-hooks/insert.js:35:1 I20160402-16:04:42.482(-4)? at Array.forEach (packages/es5-shim/.npm/package/node_modules/es5-shim/es5-shim.js:417:1) I20160402-16:04:42.483(-4)? at Function. .each. .forEach (packages/underscore/underscore.js:105:1) I20160402-16:04:42.483(-4)? at after (packages/matb33_collection-hooks/insert.js:34:1) I20160402-16:04:42.483(-4)? at Object.CollectionHooks.defineAdvice.self (packages/matb33_collection-hooks/insert.js:49:1) I20160402-16:04:42.485(-4)? at Object.collection.(anonymous function) [as insert] (packages/matb33_collection-hooks/collection-hooks.js:117:1) I20160402-16:04:42.485(-4)? at [object Object].Mongo.Collection.(anonymous function) [as insert] (packages/mongo/collection.js:590:1) I20160402-16:04:42.485(-4)?
at AccountsServer.Ap.insertUserDoc (accounts_server.js:1248:25) I20160402-16:04:42.486(-4)? at createUser (password_server.js:980:25)

Does anyone have any idea what exactly is happening?

The variable ROLES isn't defined in the context of your hook. It's either not a global or you're using Meteor 1.3 and you haven't exported it from the file you've defined it in and imported it into the file where you defined your hook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM