我在Meteor应用程序中有Node.js包,如何调用该函数?

时间:2015-07-15 00:39:45

标签: javascript node.js meteor yelp

我想在我的Meteor应用程序中使用此节点代码https://arian.io/how-to-use-yelps-api-with-node/,与Yelp的2.0 API接口并返回我可以使用的结果。

我已安装 meteorhacks:npm 并将相应的节点包添加到packages.json:

{
"oauth-signature" : "1.3.0",
"nonce" : "1.0.3",
"request" : "2.58.0",
"lodash" : "3.10.0"
}

这是我的.js文件

//Start IsCLient
if (Meteor.isClient) {

//Call Yelp Helper
  Template.yelper.events({
    'click button': function () {
    data = callYelp();
    }
  });
//Call Yelp Helper


//Call Yelp
  callYelp = function callYelp(user) {
    Meteor.call('callYelp', user);
  }
//End Call Yelp

}
//End IsClient



if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}



//Methods
Meteor.methods({
'callYelp': function callYelp(user) {


/* require the modules needed */
var oauthSignature = Meteor.npmRequire('oauth-signature');  
var n = Meteor.npmRequire('nonce')();  
var request = Meteor.npmRequire('request');  
var qs = Meteor.npmRequire('querystring');  
var _ = Meteor.npmRequire('lodash');

/* Function for yelp call
 * ------------------------
 * set_parameters: object with params to search
 * callback: callback(error, response, body)
 */


request_yelp = function(set_parameters, callback) {


  /* The type of request */
  var httpMethod = 'GET';

  /* The url we are using for the request */
  var url = 'http://api.yelp.com/v2/search';

  /* We can setup default parameters here */
  var default_parameters = {
    location: 'San+Francisco',
    sort: '2'
  };

  /* We set the require parameters here */
  var required_parameters = {
    oauth_consumer_key : process.env.oauth_consumer_key,
    oauth_token : process.env.oauth_token,
    oauth_nonce : n(),
    oauth_timestamp : n().toString().substr(0,10),
    oauth_signature_method : 'HMAC-SHA1',
    oauth_version : '1.0'
  };

  /* We combine all the parameters in order of importance */ 
  var parameters = _.assign(default_parameters, set_parameters, required_parameters);

  /* We set our secrets here */
  var consumerSecret = process.env.consumerSecret;
  var tokenSecret = process.env.tokenSecret;

  /* Then we call Yelp's Oauth 1.0a server, and it returns a signature */
  /* Note: This signature is only good for 300 seconds after the oauth_timestamp */
  var signature = oauthSignature.generate(httpMethod, url, parameters, consumerSecret, tokenSecret, { encodeSignature: false});

  /* We add the signature to the list of paramters */
  parameters.oauth_signature = signature;

  /* Then we turn the paramters object, to a query string */
  var paramURL = qs.stringify(parameters);

  /* Add the query string to the url */
  var apiURL = url+'?'+paramURL;

  /* Then we use request to send make the API Request */
  request(apiURL, function(error, response, body){
    return callback(error, response, body);
  });
};


}

})

一切似乎都包含正确,我相信我已经设置好了,将代码添加到方法中,然后按下按钮调用它。

我的问题是,在callYelp方法中,有一个创建的函数向Yelp发出实际请求,我不知道如何触发它。

您可以在我的代码中的评论中看到我试图让它发挥作用。

这是范围问题吗? Node有问题吗?

另外我知道我还没有设置我的Yelp API参数(令牌等),我会添加它们,我只是想让它至少连接并首先抛出错误。< /强>

request(apiURL, function(error, response, body){
    return callback(error, response, body);
  });

1 个答案:

答案 0 :(得分:0)

我相信您可以使用httphttp://docs.meteor.com/#/full/http拨打电话,请记住在使用之前添加包:

meteor add http

请记住在拨打电话之前取消阻止,否则您的流星过程将被阻止。这是meteor文档页面中的示例:

Meteor.methods({checkTwitter: function (userId) {
  check(userId, String);
  this.unblock();
  try {
    var result = HTTP.call("GET", "http://api.twitter.com/xyz",
                           {params: {user: userId}});
    return true;
  } catch (e) {
    // Got a network error, time-out or HTTP error in the 400 or 500 range.
    return false;
  }
}});