Search code examples
javascriptmongodbmeteoruser-accounts

Why are the admin email and meteor.user().email not equal when logged in as admin?


Currently it is supposed to check if the user has the admin email before being able to post. It instead does not allow the any user to post. I think I set the useremail variable wrong as it returns a error that the admin user does not have the right email when logged in as the admin. Why is this not working and how can it be fixed?

The error being returned .

if (post.useremail!="admin@gmail.com")
  errors.useremail =  "Please sign into a admin account to post";

Where useremail is set.

 var post = _.extend(postAttributes, {
   useremail: user.email,
   author: user.username,
   submitted: new Date(),
   commentsCount: 0
 });

Where the admin user is created the first time the server starts.

 Accounts.createUser({
     username: 'admin',
     email: 'admin@gmail.com',
     password: 'admin'
 });

Solution

  • I am not sure, that this will solve all of your problems, but here is one thing. if you, and I just assume here, get the current user data through var user = Meteor.user(); when you create your post, the user.email will be undefined (if you don't store the email there explicitly). console.log(user) to see how the objects looks like. if that is the problem here, then the code should look more like this

    useremail: user.emails[0].address
    

    lemme know if that was the problem.