Inserting HTTP.call result into a collection

Hi,

Each time a user logs in successfully to my app I want to call a function that gets the user’s friends list from facebook and then inserts it to the user collection.

on the client i call an on login function:

Accounts.onLogin(function(){
  const userId = Meteor.userId();
  const user = Meteor.users.findOne(userId);
  const fId = user.services.facebook.accessToken;
  Meteor.call('getFriendsData', fId);
  console.log('getFriendsData - onLogin');
}); 

On the server I have a method that runs the http method:

Meteor.methods({
    getFriendsData: function(fbAccessToken) {
       var data = HTTP.call("GET", 'https://graph.facebook.com/me/friends', { params: { access_token: fbAccessToken } },(error, results) => {
         resName = results.data.data; 
         Meteor.users.insert( { _id: this.userId }, {friends: resName} ); 
      });
      }
    }); 

The friends list returns correctly ( if i console.log() ) it i see the data, but when i try to insert it to the collection i get the following eror:

“Exception in callback of async function: TypeError: callback is not a function”

Is my problem the facte that i try to insert if from the call back? is it my insert and how i wrote it?
Other suggestions how can i insert the data to the collection?

FYI, the data returns as an array of objects…

Many thanks!

I think you probably want something more like this:

Meteor.methods({
  getFriendsData(fbAccessToken) {
    const results = HTTP.call("GET", 'https://graph.facebook.com/me/friends', { params: { access_token: fbAccessToken } });
    const resName = results.data.data; // You should check this - it doesn't look correct to me
    Meteor.users.update({ _id: this.userId }, { $set: { friends: resName } });
  },
});
1 Like

Thanks!

But its wierd, i don’t get an indication that it was inserted…

when i console.log() the resName i do get the results i wanted

Meteor.methods({
  getFriendsData(fbAccessToken) {
    const results = HTTP.call("GET", 'https://graph.facebook.com/me/friends', { params: { access_token: fbAccessToken }});
    const resName = results.data.data;
    console.log(resName);
    Meteor.users.update({ _id: this.userId }, { $set: { friends: resName } });
  },
});

RESULT: [{ name: ‘Amir Rosner’, id: ‘…’ } ]

but then i don’t get the indication of the update, nor do i see the field added to the user when i Meteor.users.find()…

any idea why?

thanks!!

If you do not have an explicit publication for users, you will only get the ‘profile’, ‘username’ and ‘emails’ fields sent to the client.

Have you checked the collection contents from the mongo shell?

1 Like

Yay, working.

thanks, I forgot about the publications thing.
On the shell i can see it.

Thanks, it helped a lot :slight_smile:

1 Like