简体   繁体   中英

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

Please assist with upserting results from foursquare's venues API into a new Mongo collection. The below is what I have so far:

 <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>

Also, would I need to add the API OAuth anywhere for the query to work? Should I use something like .getJSON(config.apiUrl + to query the API rather?

Thank you.

You can use oleo:foresquare Pacakge

And do something like this.

First install

meteor add oleh:foursquare

Second put the credentials

//server/secret.js

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

Third do a venue query.

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

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

Source Code DEMO OR Online DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM