Creating Collections using APIs & Internet in General

Hi, I’m fairly new to writing web applications, so please be as gentle as possible in regard to answers.

How do you create collections from data delivered from an api such as Google’s Map api. For example, if you wanted to create a MongoDB collection of all the phone numbers of coffee shops in your state, how do you create a js script that searches through Google’s Map API to locate all coffee shops in your state while returning the phone numbers of those shops to a collection? Is this something that is done using javascript? If not, what language completes this? I’ve seen some developers online make mention of things like AJAX and HTTP requests, but I’m not very familiar with these methods.

How are queries like this completed and placed into collections? This question is not at all specific to coffee shops or scaling such as a state. It was the only way I could think of framing how one could make a request to the internet, retrieve a specific subset of information, and then store that information. In a way, generating one’s own database of info that could be used further in a web app. Thanks so much for any support on this topic!

  • create a meteor app
  • in the server you will make an HTTP API call to google map api, maybe using a helper pacakge like this
let results; 

search(options, function(error, response){
results = response
})
  • this will (give or take) return an array of objects. so in the above, response will be a huge array, but can be thought of looking something like:
[ 
  { name: "johns coffee", location: "44 low street" }, 
  { name: "bobs coffee", location: "34 low street" }, 
  { name: "beths coffee", location: "48 low street" }, 
]
  • do a forEach loop of that array of objects, and insert each object into your collection
results.forEach( item => {
      CoffeeShops.insert( item )
})
  • if you want to validate before inserting into your collection (in other words, make sure each property on the object you are inserting is the correct type, etc), use something like simple schema
  • and typically you’ll also at some point declare the CoffeeShops collection ahead of time.

export const CoffeeShops = new Mongo.Collection('CoffeeShops');

Its basically all javascript but technically there are other things at play (HTTP calls/protocol, mongoDB)

AJAX is just esoteric programmer speak for “send a request from my browser to a “sever”. sometimes you do this to get a response back with data, other times you are sending data to the server for the server to insert into a DB”

In general, apps are:

  • the client-side (the code that runs in your browser)
  • the server (which in most cases is the middle man between the browser and a database, or many databases. its basically code that runs on some powerful computer with no monitor in the nevada desert)
  • the database (which is a glorified spreadsheet)
1 Like

This is the answer I’ve been looking for! Thank you for writing this!