Kostenko
Kostenko

Reputation: 307

Store Facebook avatar in MongoDB using Meteor

I'm trying to build my first Meteor app following the tutorial on the official site. I use the accounts-ui and the accounts-facebook packages.

When a new user logins I want to store his avatar in the database.

What I have done so far:

// Insert a task into the collection

  Tasks.insert({

    text: text,

    createdAt: new Date(), // current time

    owner: Meteor.userId(),           // _id of logged in user

    username: Meteor.user().username || Meteor.user().profile.name , // username of logged in user

    avatar: Meteor.user().profile.picture

  });

And in HTML:

<template name="task">

  <li class="{{#if checked}}checked{{/if}}">
    <button class="delete">&times;</button>
    <img class="avatar" src="{{avatar}}">
    <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
    <span class="text"><strong>{{username}}</strong></span>
    <span class="text">{{text}}</span>

  </li>

</template>

If the user logins with Facebook everything is fine, but if he tries to login with a custom user name I get the error:

Meteor.user(...).profile is undefined

What I want to achieve :

avatar: Meteor.user().profile.picture // if the user has a Facebook account

OR

avatar: "img/default" // if the user uses custom username

I already tried:

avatar: Meteor.user().profile.picture || avatar: "img/default"

But it does not work.

Is there a way to achieve this?

Upvotes: 0

Views: 239

Answers (1)

user5084201
user5084201

Reputation:

@kostenko you should read more about how to access object properties in javascript.

and here is the solution from your last try it was close:

var user = Meteor.user();
avatar: user && user.profile && user.profile.picture || "img/default";

Upvotes: 2

Related Questions