Last active
September 2, 2018 16:35
-
-
Save devjourney/3993db636d9670d6fb7fd9b6657a2924 to your computer and use it in GitHub Desktop.
A Node.js function to fetch weather station data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let documentClient = require('documentdb').DocumentClient; | |
let cosmos_uri = process.env["STATION_COSMOS_URI"]; | |
let cosmos_key = process.env["STATION_COSMOS_READONLY_KEY"]; | |
let databaseId = process.env["STATION_COSMOS_DATABASE_NAME"]; | |
let collectionId = process.env["STATION_COSMOS_COLLECTION_NAME"]; | |
let client = new documentClient(cosmos_uri, { 'masterKey': cosmos_key }); | |
let collectionLink = "/dbs/" + databaseId + "/colls/" + collectionId + "/"; | |
module.exports = function (context, req) { | |
let filterQuery = `SELECT * FROM c WHERE c.State = "${req.params.state}"`; | |
try { | |
let queryIterator = client.queryDocuments(collectionLink, filterQuery); | |
queryIterator.toArray(function (err, matchingDocuments) { | |
if (err) { | |
context.done(err); | |
return; | |
} | |
context.done(null, { status: 200, body: matchingDocuments, headers: { 'Content-Type': 'application/json' }}); | |
}); | |
} catch (ex) { | |
context.done(ex); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment