Advertisement
alessandromendozza

Firebase function to search

Dec 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const functions = require('firebase-functions');
  2. const admin = require('firebase-admin');
  3. admin.initializeApp(functions.config().firebase);
  4. // // Create and Deploy Your First Cloud Functions
  5. // // https://firebase.google.com/docs/functions/write-firebase-functions
  6. //
  7. // exports.helloWorld = functions.https.onRequest((request, response) => {
  8. //  response.send("Hello from Firebase!");
  9. // });
  10.  
  11. // Docs: https://firebase.google.com/docs/functions/http-events
  12. // use this for calling this function: curl -X POST -H "Content-Type:application/json"  -d '{"city":"Naples"}' https://us-central1-sportme-4e614e.cloudfunctions.net/getMatch?city=Naples
  13. exports.getMatch = functions.https.onRequest((req,res) => {
  14.     const myCity = req.query.city
  15.     console.log("Matching for my city: " + myCity)
  16.     b = [ 5, 5 ]
  17.     var match = []
  18.     admin.database().ref().child('users/').once('value').then(function(snapshot) {
  19.         //for ( u in snapshot.val() ) {
  20.         snapshot.forEach(function(user){
  21.             //console.log( user.key )
  22.             //console.log( user.val() )
  23.             // Only matches people from my city
  24.             //console.log("  - " + user.val().city )
  25.             if ( myCity.localeCompare( user.val().city ) == 0 ) {
  26.                 var x = b[0]*user.val().fitness + b[1]*user.val().goal
  27.                 var obj = { mVal: x, user: user.key }
  28.                 //console.log("  ++ " + obj)
  29.                 match.push( obj )
  30.                 //console.log(match)
  31.             }
  32.             //})
  33.         })
  34.         match.sort(function(a,b){
  35.             return a.mVal < b.mVal
  36.         })
  37.         //console.log(match)
  38.        
  39.         res.status(200).send(match)
  40.     })
  41.     //console.log("match result" + match)
  42. })
  43.  
  44. //TODO: when matching by tags, if there's no match, just proceed with matching by city
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement