使用Meteor.js将Foursquare API的搜索结果添加到新的Mongo集合中

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

本文关键字:Mongo 集合 添加 搜索结果 js Foursquare API 使用 Meteor      更新时间:2023-09-26

请协助将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 package

然后像这样做。

第一次安装

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
             });
          }
        }
      });
    }
   }
 })

源代码演示或在线演示