I am using the firebase admin sdk with bubble as a nodejs project
here is the code i am trying. THIS doesn't work. I have included updated code at the bottom but will leave this for now hoping others can learn from my errors.
function(properties, context) {
console.log('trying')
var list = []
var util = require("util")
var admin = require('firebase-admin');
var serviceAccount = <COPIED/PASTED FROM FIREBASE>
// Fetch the service account key JSON file contenT
if (admin != null) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://login-database-brc.firebaseio.com"
})
list.push('logged into app')
var db = admin.database();
var ref = db.ref('BRC/4jV39aEK6jOecLdKONJMKatsEYn1/data')
ref.once('value').then((snapshot) => {
snapshot.val().forEach(element => {
list.push('check')
list.push(element.Name)
})
});
} else {
list.push('nothing happened')
}
// As an admin, the app has access to read and write all data, regardless of Security Rules
return {
data: list
}
}
here are the rules
{
"rules":{
"BRC": {
"$uid":{
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
I only end up seeing 'logged in' in the output list. I would expect to see the name property from each object in the data bucket.
I am able to make this work fine client side using the standard sdk, and using password authentication. I can’t seem to get beyond initializing the apps Firebase instance.
i do have /BRC with rules that require authentication, but with the server SDK i didnt think this mattered
I want to access all ojects held within BRC/4jV39aEK6jOecLdKONJMKatsEYn1/data
[UPDATED CODE TO PERFORM OPERATIONS SYNCRONOUS]
function(properties, context) {
var serviceAccount = {'jsonObjectFromFirebase'};
var list =[];
var admin = require("firebase-admin");
// Initialize the app with a service account, granting admin privileges
if (!admin.apps.length){
initializeApp(serviceAccount)
};
// As an admin, the app has access to read and write all data, regardless of Security Rules
admin.database().ref('test').once('value')
.then((snapshot) => {
snapshot.val().forEach(element => {
let a = element.name;
list.push(a);
})
return_data(list)
});
function return_data (list){
return { data: list};
};
function initializeApp (serviceAccount){
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://thunkableproject-f51d9-default-rtdb.firebaseio.com"
});
}
}
the issue is that now return_data is never called. so I'm not sure what's happening here.
[update 2, trying again to create a synchronous call
var list =[]
var admin = require("firebase-admin");
// Initialize the app with a service account, granting admin privileges
if (!admin.apps.length){
initializeApp(serviceAccount)
}
// As an admin, the app has access to read and write all data, regardless of Security Rules
admin.database().ref('BRC/4jV39aEK6jOecLdKONJMKatsEYn1/data').once("value", function(snapshot){
let element = snapshot.val()
let list = []
for (var key in element) {
list.push(element[key].Name)
}
return_data(list)
})
function return_data (list){
return { data: list}
}
function initializeApp (serviceAccount){
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://login-database-brc.firebaseio.com"
});
}
I have also tried this to no avail
var promise = admin.database().ref('BRC/4jV39aEK6jOecLdKONJMKatsEYn1/data').once("value");
promise.then(snapshot => {
let element = snapshot.val()
let list = []
for (var key in element) {
list.push(element[key].name)
};})
.catch(error => {
alert(error)
});
return { data: list}
Update 9:23 EST 12/29/20
I think I now am calling this function in an asynchronous way that should wait for a callback. the issue now is that the code in the .then() never executes.
function getData() {
var promise = admin.database().ref('BRC/4jV39aEK6jOecLdKONJMKatsEYn1/data').once("value");
promise.then(snapshot => {
let element = snapshot.val()
let list = []
for (var key in element) {
list.push(element[key].Name)
};
return list;
})
.catch(error => {
return error
});
return promise;
}
getData().then((list) => {
return { "data": list }
})