Users in my Meteor app can create accounts 'manually' or with the accounts-facebook package.
If they create an account manually then in the database their email is stored like this:
emails: [address: '[email protected]', verified: false]
But if they use the Facebook login then its stored like this:
services: {
facebook: {
email: "[email protected]"
}
}
I have an user account page where I need to display the users email and allow them to change it. How can I deal with the varying structure?
I made this React component to display the users email. When I just had the default Meteor user profiles it worked, but now Ive added Facebook login it errors as props.user.emails doenst exist.
<div className="form-primary__row">
<label>Email:</label>
{props.user.emails.map((item, i) => {
return (
<input defaultValue={item.address} key={i} name="email" />
);
})}
</div>
This is my method for a user to update their email. It also worked when I just had Meteors accounts but won't work with Facebook.
Meteor.methods({
'user.updateEmail'({ email }) {
Meteor.users.update(
{ _id: Meteor.userId() },
{
$set: {
'emails.0.address': email,
'emails.0.verified': false,
},
},
);
},
});
services.manual.emailAssuming that there can only exist either manual emails OR fb emails, you can program your logic to return like either of the two, based on whichever exists. I believe this can be normalized on the client code and sent back as a standard object to the client so that the client need not bother about the structure of the object it received. - bluerenAccounts.onCreateUser(). When a new user is created (irrespective of manual or via fb), pull the respective email and populate theemailsarray. That way, you don't have to touch your publications or your client side rendering - blueren