Giter VIP home page Giter VIP logo

Comments (8)

alanning avatar alanning commented on July 30, 2024

Hi Shkumbini,

I think what's happening is that since you are running this on the client, the call to 'Accounts.createUser' is async so it will immediately return and your id will be undefined (or maybe null). You can verify this by logging the value right before the Roles call.

Move this code to a server-side Meteor method which you call from your #signup click event handler and it should work.

Cheers,
Adrian

Sent from my phone

On Apr 23, 2014, at 11:06 AM, Shkumbini [email protected] wrote:

Hi there

I'm trying to set a sign up form but this error comes in Chrome Inspect when I click Sign Up, here's the code:

Template.signUp.events({
'click #signup': function (e, t) {
e.preventDefault();

var users = [
    {
        name: t.find('#fullName').value,
        email: t.find('#email').value,
        roles:['admin']
    },
    {
        name:"View-Secrets User",
        email:"[email protected]",
        roles:[
            'view-secrets'
        ]
    }
];

_.each(users, function (user) {
    var id;

    id = Accounts.createUser({
        email: user.email,
        password: t.find('#password').value,
        profile: {
            name: user.name
        }
    });

    if (user.roles.length > 0) {
        // Need _id of existing user record so this call must come
        // after `Accounts.createUser` or `Accounts.onCreate`
        Roles.addUsersToRoles(id, user.roles);
    }

});

}
});


Reply to this email directly or view it on GitHub.

from meteor-roles.

shkumbin avatar shkumbin commented on July 30, 2024

Yeah, I thought so, but how I'm gonna run client side events in server side. At least cannot find the method to run it in server side. Thanks a lot...

from meteor-roles.

alanning avatar alanning commented on July 30, 2024

It's a meteor thing. You define a meteor method in your server side code that does the new user creation and roles update. Then you call that from the client side event handler.

Meteor.methods(...)
Meteor.call(...)

Typing from phone so can't give real example but meteor docs and examples show how to use meteor methods and calls.

This SO answer may also be helpful:
http://stackoverflow.com/questions/10343656/meteor-how-to-call-a-method-defined-in-meteor-methods

A

Sent from my phone

On Apr 23, 2014, at 5:02 PM, Shkumbini [email protected] wrote:

Yeah, I thought so, but how I'm gonna run client side events in server side. At least cannot find the method to run it in server side. Thanks a lot...


Reply to this email directly or view it on GitHub.

from meteor-roles.

evandertino avatar evandertino commented on July 30, 2024

I am having the same problem, please help. I don't know what am doing wrong

My code is below.

//collections/users.js

Meteor.methods({
    mCreateUser: function (user) {

        if(_.isObject(user)) {

            if (user.username) {

                var id = Accounts.createUser({
                    username: user.username,
                    email: user.email,
                    password: user.password
                });

                if (user.roles.length > 0) {
                    // Need _id of existing user record so this call must come 
                    // after `Accounts.createUser` or `Accounts.onCreate`
                    //[].concat(user);
                    Roles.addUsersToRoles(id, user.roles);
                }

                _.extend(user, {id: id});

                return user;
            }
        }
    }
});

//views/register.js

var newUserData = {
    username: username,
     email: email,
     password: password,
     roles: ['customer']
};

Meteor.call('mCreateUser', newUserData, function (error, result) {
    if (error) {
         console.log(error);
          return;
     }
     console.log(result);
}

from meteor-roles.

alanning avatar alanning commented on July 30, 2024

Hi @evandertino,

Please put together a simple reproduction of the issue. (If you'd like help on how to make the reproduction let me know.)

Once its up on Github, post a link here and I'll help debug it.

from meteor-roles.

evandertino avatar evandertino commented on July 30, 2024

Hey @alanning,

Yes, I already setup a simple reproduction, of my issue.

You can check it out here

Thank you.

from meteor-roles.

alanning avatar alanning commented on July 30, 2024

Hi @evandertino,

The error that I see displayed is Chrome in your reproduction is:

Exception while simulating the effect of invoking 'rolesCreateUser' 
Error
 Error: Missing 'users' param

This is because you are exposing your 'rolesCreateUser' method on both the client and the server so it is attempting to simulate the method call on the client in parallel to executing it on the server.

The one on the client throws that error because on the client the Accounts.createUser call just prior to the Roles.addUsersToRoles call does not immediately add the new user to the client collection. Rather it does it on the server side so there is a delay before that change is propagated back to the client.

So on the client, when the Roles.addUsersToRoles call is called it checks for the user by _id in the local, client-side collection and doesn't find it, thus throwing the error.

To fix this issue, move your rolesCreateUser meteor method definition to be server-side only. This will prevent the Meteor client from trying to simulate it. You can do that by moving the collections/users.js file to server/collections/users.js.

from meteor-roles.

evandertino avatar evandertino commented on July 30, 2024

Thank you so much, your explanation was very clear.

from meteor-roles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.