Skip to content

Instantly share code, notes, and snippets.

Last active October 1, 2018 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pca2/399bf17084b25fbfa58dc0a5c364b43d to your computer and use it in GitHub Desktop.
Save pca2/399bf17084b25fbfa58dc0a5c364b43d to your computer and use it in GitHub Desktop.
Combining MongoDB Stitch Triggers and Services
<script src=""></script>
// Initialize the App Client
const client = stitch.Stitch.initializeDefaultAppClient("INSERT-YOUR-APP-ID");
// Get a MongoDB Service Client
const mongodb = client.getServiceClient(
// Get a reference to the blog database
const db = mongodb.db("Weight");
function displayWeights() {
.find({}, { limit: 1000 })
.then(docs => => `<tr><td>${doc.weight}</td></tr>`))
.then(weights => document.getElementById("weights").innerHTML = weights.join(" ") )
function displayWeightsOnLoad() {
.loginWithCredential(new stitch.AnonymousCredential())
function addWeight() {
const newWeight = document.getElementById("new_weight");
console.log("add weight",
.insertOne({ owner_id:, weight: newWeight.value })
newWeight.value = "";
<body onLoad="displayWeightsOnLoad()">
<h3>Weight Tracker 3000</h3>
<div id="content">
Keep track of your weight with our nifty website
<table id="weights"></table>
Add a new weight: <input id="new_weight" type="number"><input value="Submit Weight" type="submit" onClick="addWeight()">
exports = function(changeEvent) {
const http ="wt_http_service");
const newWeight = changeEvent.fullDocument.weight;
const thresholdWeight = 145;
const slackURL = '';
const slackMsg = `Uh-oh a weight was posted of ${newWeight}, better lay off the pie`;
if(newWeight < thresholdWeight ){
return "Weight under threshold";
} else {
return http
.post({ url: slackURL, body: JSON.stringify({"text": slackMsg})})
.then(resp => {
return resp;
.catch(err => console.log(err) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment