Hey there, to all the amazing folks at stackoverflow who have been supporting me over the years, I have a new question for you!
I've been delving into Android programming for quite some time now and I enjoy exploring different ways to optimize apps. Recently, I stumbled upon Firebase Realtime Database and its feature of triggering server-side entries with functions. I've watched numerous videos on Firebase, sifted through their pages, and even experimented with Typescript.
Unfortunately, my English skills are severely lacking and I'm struggling to find examples that can help me get a simple code snippet up and running. I managed to create a Node.js code, tested it with firebase emulators locally which worked fine, but encountered issues when deploying it to the Google server using "Firebase deploy". My main goal is to make an entry in another path after triggering an initial entry. The concept eludes me and I'm hoping someone can shed light on what could be wrong and how to rectify it.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
const serviceAccount = require('../serviceAccount.json')
let userId = "";
admin.initializeApp({
credential: admin.credential.applicationDefault() ,
databaseURL:`https://${serviceAccount.project_id}.firebasio.com`
});
// const db = admin.database();
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
export const helloWorld = functions.https.onRequest((request, response) => {
console.log("hallo world");
response.send("Hello from Firebase!");
});
export const setPlayerInTeam = functions.database.ref('{userId}/team/{teamId}/allPlayer/{spielerId}/')
.onCreate( (snapshot,context)=>{
const original = snapshot.val();
userId = context.params.userId;
console.log('userId',userId);
console.log('teamId',context.params.teamId);
console.log('spielerId',context.params.spielerId);
console.log('original.name',original.name);
console.log('Updates',context.params.spielerId," "+context.params.teamId);
const adaNameRef = admin.database().ref();
adaNameRef.child('test').push({ first: 'Ada', last: 'Lovelace' }).then(function() {
console.log('Synchronization succeeded');
})
.catch(function(error) {
console.log('Synchronization failed');
});
return adaNameRef;
});
Experimenting with async produced the same result according to the Console feedback.
.onCreate(async (snapshot,context)=>{
......
await adaNameRef.child('test')
The Powershell Console output looks like this:
userId TZBAxFGSgZPZBoYZz7F4MVm5MAP2
> teamId -M797y0K4BBCEOa_nVoB
> spielerId -M7skpftMOOF4QlEW2kv
> original.name luigi
> Updates -M7skpftMOOF4QlEW2kv -M797y0K4BBCEOa_nVoB
i functions: Finished "setPlayerInTeam" in ~1s
> Synchronization succeeded
In the Firebase Console Log, I see the following:
Function execution started
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
userId TZBAxFGSgZPZBoYZz7F4MVm5MAP2
teamId -M8e9ro1Dlb7VA9Pab4t
spielerId -M8e9ozdo0uU6NCXfG2z
original.name Ayla
Updates -M8e9ozdo0uU6NCXfG2z -M8e9ro1Dlb7VA9Pab4t
Function execution took 1151 ms, finished with status: 'ok'
No success, no errors?
Apologies for the rough English translation, everything was done using Google Translate. Thank you in advance!