Skip to content

Instantly share code, notes, and snippets.

@esafwan
Last active July 31, 2018 19:58
Show Gist options
  • Save esafwan/f7b8b512e36d00327a0a215a5243c640 to your computer and use it in GitHub Desktop.
Save esafwan/f7b8b512e36d00327a0a215a5243c640 to your computer and use it in GitHub Desktop.
Query firebase from functions with express to filter item by a alphanumerical child value.
/*
CHAIN MULTIPLE QUERIES USING PROMISES IN FIREBASE REALTIMEDB
FROM EXPRESS IN FIREBASE FUNCTIONS
*/
ss.post("/test", (request,response) => {
//Get data
let shakes = request.body.shakes
let uid = request.body.uid
//The array to chain promises
let promises = [];
//Load coupons and gifts/
promises.push(db.ref('/gifts').orderByChild("active").equalTo("true").once('value'));
promises.push(db.ref('/coupons').orderByChild("active").equalTo("true").once('value'));
//Load users current data (todays count from timestamp, gifts, vouchers)
promises.push(db.ref('/shakes_user/' + uid).once('value'));
//Load users current gift_allowed status. (If false, user can't anymore gifts)
promises.push(db.ref('/shakes_user/' + uid + "/gift_allowed").once('value'));
// Wait for all promises to resolve
Promise.all(promises).then(function(res) {
// res[0] is gifts.
// res[1] is coupons.
// res[2] is current users data
// res[3] is current users gift_allowed status
let gifts = res[0]; //gift
let coupons = res[1]; //coupon
let user_data = res[2];
let gift_allowed = res[3];
//Array for merging both gift and coupons
let merged = [];
//Add gifts to the array only if the gift_allowed is true
if (gift_allowed) {
//Loop through gift and push items to the array.
gifts.forEach(function(item){
merged.push(item);
})
response.send("worked").end();
}
//Loop through coupons and push items to the array.
coupons.forEach(function(item){
merged.push(item);
})
//Get random value from the available coupons/gifts.
var random = merged[Math.floor(Math.random() * merged.length)];
response.send(random);
});
});
[ {
"expires" : "1541901660",
"id" : "gift123",
"gift" : {
"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non nisi blandit, tincidunt dui ut, facilisis diam.",
"img" : "gift.png",
"name" : "Gift Name"
},
"terms" : "Lorem ipsum dolor.",
"type" : "gift"
}, {
"expires" : "1541901660",
"id" : "honda_activa_1",
"gift" : {
"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non nisi blandit, tincidunt dui ut, facilisis diam.",
"img" : "gift.png",
"name" : "Honda Activa"
},
"terms" : "Lorem ipsum dolor.",
"type" : "gift"
}, {
"expires" : "1541901660",
"id" : "ipad_2018",
"gift" : {
"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non nisi blandit, tincidunt dui ut, facilisis diam.",
"img" : "gift.png",
"name" : "Ipad Pro"
},
"terms" : "Lorem ipsum dolor.",
"type" : "gift"
}, {
"expires" : "1541901660",
"id" : "ipad_2018",
"gift" : {
"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non nisi blandit, tincidunt dui ut, facilisis diam.",
"img" : "gift.png",
"name" : "Macbook Pro 15"
},
"terms" : "Lorem ipsum dolor.",
"type" : "gift"
} ]
/*
ACCESS FIREBASE REALTIMEDB FROM FIREBASE FUNCTIONS
USING EXPRESS.JS
*/
const express = require("express")
var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
ss.get("/gift", (request, response) => {
admin.database().ref('gifts').orderByChild('id').equalTo("gift123").once('child_added').then(snapshot => {
//console.log(snapshot.val())
response.send(snapshot.val())
})
})
const api = functions.https.onRequest(ss)
module.exports = {
api
}
/*
LOAD A ITEM IN FIREBASE REALTIMEDB USING AN ID STORED IN
IT AS CHILD.
*/
//Get gift_id from post
let gift_id = request.body.gift_id
//Get from firebase by the gift_id
db.ref("gifts").orderByChild("id").equalTo(gift_id).once("value",snapshot => {
//Check if data is returned
if (snapshot.exists()){
response.send(snapshot.val())
} else {
response.send("Gift id " + gift_id + "doesn't exist.")
}
});
@esafwan
Copy link
Author

esafwan commented Jul 26, 2018

How it looks in firebase:

The firebase view of the db.json

Postman screenshot of the API in action:

Postman screenshot of the API in action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment