Skip to content

Instantly share code, notes, and snippets.

@mabez
Last active September 1, 2017 17:53
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 mabez/5ab18e74e7c60f4209bc0c11a2594b5f to your computer and use it in GitHub Desktop.
Save mabez/5ab18e74e7c60f4209bc0c11a2594b5f to your computer and use it in GitHub Desktop.
Uma function do google cloud que conecta com o firebase
var functions = require('firebase-functions');
var admin = require('firebase-admin');
var code = 404;
var result = "{\"message\": \"not found\"}";
var debug = '';
admin.initializeApp(
{...},
databaseURL: "https://amigosecreto-7d398.firebaseio.com/"
});
exports.amigos = functions.https.onRequest((req, res) => {
var db = admin.database();
var ref = db.ref("amigos");
res.set('content-type','application/json');
if (req.method == 'GET') {
if (req.query.id != {}) {
ref.once("value", function(snapshot) {
var lista = snapshot.val();
result = "{\"message\": \"" + req.query.id + " not found\"}";
if (typeof lista[req.query.id] !== 'undefined' && lista[req.query.id]) {
code = 200;
result = JSON.stringify(lista[req.query.id]);
}
});
res.status(code).end(result);
}
ref.once("value", function(snapshot) {
code = 200;
result = JSON.stringify(snapshot);
});
res.status(code).end(result);
}
var message = "{\"message\": \"" + req.method + " not implemented\"}";
res.status(405).end(message);
});
{
"name": "amigos",
"version": "0.0.1",
"dependencies": {
"firebase-functions": "0.6.3",
"firebase-admin": "5.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment