从Facebook API将数据插入Meteor [英] Insert data to Meteor from Facebook API

查看:96
本文介绍了从Facebook API将数据插入Meteor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 here 上的示例FB Graph的数据。到目前为止,我已经设法从FB中提取数据,但是我无法弄清楚如何将其插入到MongoDB中。

I'm following the example given here on pulling data from FB Graph. So far I've managed to pull data from FB however I can't figure out how to insert it into MongoDB.

现在来自Facebook的数据呈现如下:

Right now the data from Facebook renders as follows:

{data :[
{
picture:https://photo.jpg,
id:1234,
created_time:2013-01- 01T00:00:00 + 0000
},
{
图片:https://photo.jpg,
id:12345,
created_time:2013-01-01T00:00:00 + 0000
}]
}

我创建了一个集合调用照片,我已经尝试使用 Photos.inser(data)插入数据。这是我的服务器代码:

I created a collection call Photos and I've tried to insert the data with Photos.inser(data). Here is my server code:

 function Facebook(accessToken) {
    this.fb = Meteor.require('fbgraph');
    this.accessToken = accessToken;
    this.fb.setAccessToken(this.accessToken);
    this.options = {
        timeout: 3000,
        pool: {maxSockets: Infinity},
        headers: {connection: "keep-alive"}
    }
    this.fb.setOptions(this.options);
}

Facebook.prototype.query = function(query, method) {
    var self = this;
    var method = (typeof method === 'undefined') ? 'get' : method;
    var data = Meteor.sync(function(done) {
        self.fb[method](query, function(err, res) {
            done(null, res);
        });
    });
    return data.result;
}

Facebook.prototype.getUserData = function() {
    return this.query('me');
}
Facebook.prototype.getPhotos = function() {
    return this.query('/me/photos?fields=picture');
}

Meteor.methods({
    getUserData: function() {
        var fb = new Facebook(Meteor.user().services.facebook.accessToken);
        var data = fb.getUserData();
        return data;
        _.forEach(data.data, function(photo) {
            Photos.insert(photo, function(err) { 
                if(err) console.error(err); 
            });
        });
    }
});

这是我的收藏代码:

Photos = new Meteor.Collection('picture');

现在,没有任何东西被插入到MongoDB中,我无法弄清楚为什么。

Right now, nothing is being inserted into MongoDB and I can't figure out why.

有什么想法在这里做错了,还是有更好的方式来解决这个问题?
提前感谢!

Any thoughts on what I'm doing wrong here or if there is a better way to approch this problem? Thanks in advance!

推荐答案

只是想让它工作。非常感谢戴夫和em e您的帮助!以下是那些有同样问题的人的代码。另外,请让我知道,如果有更好的方法来解决这个问题。

Just figured got it to work. Many thanks to Dave and emgee for your help! Below is the code for those who are having the same issue. Also, please let me know if there is a better way to go about this.

服务器代码:

function Facebook(accessToken) {
    this.fb = Meteor.require('fbgraph');
    this.accessToken = accessToken;
    this.fb.setAccessToken(this.accessToken);
    this.options = {
        timeout: 3000,
        pool: {maxSockets: Infinity},
        headers: {connection: "keep-alive"}
    }
    this.fb.setOptions(this.options);
}

Facebook.prototype.query = function(query, method) {
    var self = this;
    var method = (typeof method === 'undefined') ? 'get' : method;
    var data = Meteor.sync(function(done) {
        self.fb[method](query, function(err, res) {
            done(null, res);
        });
    });
    return data.result;
}

Facebook.prototype.getUserData = function() {
    return this.query('me/photos');
}

Meteor.methods({
    getUserData: function() {
        var fb = new Facebook(Meteor.user().services.facebook.accessToken);
        var data = fb.getUserData();
        _.forEach(data.data, function(photo) {
            Photos.insert(photo, function(err) { 
                if(err) console.error(err); 
            });
        });
    }
});

Meteor.publish('picture', function() {
  return Photos.find();
});

集合:

Photos = new Meteor.Collection('picture');

客户端:

Meteor.subscribe('picture');

Template.facebookphoto.helpers({
        pictures: function () {
             return Photos.find();
          }     
});

Template.fbgraph.events({
    'click #btn-user-data': function(e) {
        Meteor.call('getUserData', function(err, data) {
             $('#result').text(EJSON.stringify(data, undefined, 4));
         });
    }
});

HTML模板:

<template name="fbgraph">
    <div id="main" class="row-fluid">
      {{> facebookphoto}}
    </div>

    <button class="btn" id="btn-user-data">Get User Data</button>
    <div class="well">
        <pre id="result"></pre>
    </div>
</template>


<template name="facebookphoto">
  <div class="photos">
    {{#each pictures}}
      {{> photoItem}}
    {{/each}}
  </div>
</template>

<template name="photoItem">
  <div class="photo">
    <div class="photo-content">
      <a href="{{source}}">
       <img class="img-rounded" src="{{picture}}">
      </a>
    </div>
  </div>
</template>

这篇关于从Facebook API将数据插入Meteor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆