使用 Meteor.js 将 Foursquare API 地点搜索结果更新到新的 Mongo 集合

Upserting Foursquare API venues search results to a new Mongo Collection using Meteor.js

请协助将 foursquare 场地 API 的结果更新到新的 Mongo 集合中。以下是我目前所拥有的:

 <head>

 <title>Places from Foursquare</title>

 </head>
 <body>

 <script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
 <script>

 navigator.geolocation.getCurrentPosition(function(data) {
 var lat = data['coords']['latitude'];
 var lng = data['coords']['longitude'];

 var CLIENT_ID = 'MyClientID';
 var CLIENT_SECRET = 'MyClientSecret';


 var API_ENDPOINT = 'https://api.foursquare.com/v2/venues/search' +
 '?client_id=CLIENT_ID' +
 '&client_secret=CLIENT_SECRET' +
 '&v=20130815' +
 '&ll=' + lat + ',' + lng +
 '&query=coffee' +
 '&callback=?';


 Venues = new Mongo.Collection("venues");

 Meteor.methods({
 'fetchNearbyLocations': function(coords) {
 if (Meteor.isServer) {

 Venues.upsert(;
  });
}
}
});


</script>

</body>

此外,我是否需要在任何地方添加 API OAuth 才能使查询正常工作?我应该使用 .getJSON(config.apiUrl + 之类的东西来查询 API 而不是?

谢谢。

您可以使用 oleo:foresquare 包装

然后做这样的事情。

首次安装

meteor add oleh:foursquare

第二次放凭证

//server/secret.js

Foursquare.init({
  id: 'xxxxxxxxxxxxxxxxxxxxxxx',
  secret: 'xxxxxxxxxxxxxxxxxxxxxxx',
  authOnly: false // need auth for using or no?
})

第三次做场地查询

<template name="example">
 <input type="text" id="venueQuery">
<br>
 {{#each venus}}
    {{result}}
 {{/each}}
</template>

现在的JS。

  Venus = new Mongo.Collection(null) // client side collection to store the venus
Template.example.events({

 'keypress #venueQuery':function(event,template){

   if(event.keyCode === 13){

      params = { //query to the params,
               ll:"35.68949, 139.69171", //Your location use the geo result here
               query:template.$('#venueQuery').val(),
               limit:10, //the limit of the query
          }

     //Now the Find.
    Foursquare.find(params, function(error, result) {

      if(!error){ //if no error 

          if(result.response.venues.length === 0){ // if the query cant find anything

           console.log("nothing find");

          }else{

           queryResult = result.response.venues //Taking the venues array.

            queryResult.forEach(function(venues,index){   

             street = venues.location.formattedAddress
             lat = venues.location.lat
             lng = venues.location.lng
             city = venues.location.city
             venueName = venues.name;

            var markerData = {        
                  lat : lat,    
                  lng : lng,
                  venueName :venueName,
                  query : params.query,
                  street : street,
                  city : city,
               }

           Venues.insert(markerData) //Inserting into the client side collection
             });
          }
        }
      });
    }
   }
 })

源代码DEMO OR Online DEMO