(Closed) Insert Username in Collection with Accounts-ui

SOLVED

Hey guys. Maybe this question has come up a few times, but i can’t seem to find the right answer.

I’m developing a Social platform, where i want to save the users name in my collection. By that means, i want the logged in users username from Accounts-ui with either Facebook, Twitter etc. and save that name in my collection with Collection2.

So when i print out the author, instead of the users ID, it will instead show the users name. For example: “Nick Johnson” instead of “Hwus282HSsw82asd12”

Thanks in advance. Best regards

Fillah, the Meteor newbie

I think this article about data association in The Meteor Chef may help you

https://themeteorchef.com/snippets/simple-data-associations-in-meteor/

Hey man! Thanks for the answer.
With this example, the user has to type a name in a field. That’s not entirely what I’m looking for, but really a great and related post otherwise.

I want to automatic get the users logged in username and save it in my collection, without the user has to type their name.

For example, the only data I want from the user is description. Both username and createdAt should automatically be put in the database. But I can’t seem to get the actual name, only id

You always have access to Meteor.user objet both client side and server side.

Now I have the following: return this.userId, which returns the logged in users Id. But if I change it to: return Meteor.user it won’t submit and nothing happens. Can you tell me why?

Meteor.user returns an object with the user properties. There you can get the information you need depending on how you stored it, for example Meteor.user.profile.fullname

It says that fullname is undefined. It looks like this:

author: {
type: String,
label: “Author”,
autoValue: function() {
return Meteor.user.profile.fullname
},
autoform: {
type: “hidden”
}
}

If i remove fullname nothing happens and no error appears

Well, the reason that no error appears is simple:

Meteor.user.profile exists (even if it may be empty) and thus can be returned as a value.

Meteor.user.profile.fullname does not exist and thus returns an error.

The proper way to deal with this is:

return Meteor.user.profile.fullname ? Meteor.user.profile.fullname : "default value";

It’s a ternary for checking whether a value exists before returning it and is essentially the same as this:

if(Meteor.user.profile.fullname) {
    return Meteor.user.profile.fullname;
} else {
    return "default value";
}

Thanks for the quick reply!

It still gives me an error:

Uncaught TypeError: Cannot read property ‘fullname’ of undefined

How should it properly be set up?

PostSchema = new SimpleSchema ({
author: {
type: String,
label: “Author”,
autoValue: function() {
return Meteor.user.profile.fullname ? Meteor.user.profile.fullname : “default value”
},
autoform: {
type: “hidden”
}
}

Okay, seems that I remembered wrong and the profile field also does not exist for whatever reason.

return (Meteor.user.profile && Meteor.user.profile.fullname) ? Meteor.user.profile.fullname : "default value";

Great! Now it sets the author as “default value”. How do i then make it the logged in users name with Facebook?

For example: “Tom Hanson”

Well, I myself don’t use 3rd party services. A first step would be to do something like this:

Meteor.loginWithFacebook({
  requestPermissions: ['public_profile']
}, function (err) {
  //Breakpoint here
  if (err)
    console.log("Error: " + (err.reason || 'Unknown error'));
});

and set a breakpoint to see exactly what is returned from the service.

It says:

message: “Login service configuration not yet loaded”

And in console:

Error: unknown error

Then you didn’t set up the service properly which you have to do first.

http://docs.meteor.com/#/full/meteor_loginwithexternalservice

You have to register your app with the provider (in this case: Facebook), then you get the secret for ServiceConfiguration.configurations and then you can try again.

I can’t seem to solve this problem. On my website, the first thing the user enters is a “hello page”. Here the user signs in and gets redirected to /frontpage.

I’ve added the code you send on Frontpage.js, which should be checked after the user has signed in. But still it gives me the error. Do you have any idea if it’s misplaced or something? I could upload my project if that helps

You have to set up the service first… read the link.

Without registering your app at Facebook, this will never work.

Basically, you’re hiring Facebook to act as your bouncer to a party. Without registering at Facebook that you are the host, this will happen:

User: fillah sent me. Let me in.
Bouncer: Sorry, don’t know anyone named fillah. Go away now.

1 Like

Sorry! I’m on it now

Now i’ve added the package and the ServiceConfiguration. It gives me an error:

Error: Not permitted. Untrusted code may only update documents by ID. [403]

Without some kind of code accompanied by that message, I can’t tell you anything but that you’re trying to update several documents at once on the client. Only the server is allowed to do bulk updates.

Would it help if i pulled it to Github so you are able to see the project?