Reputation: 3
Hello stackoverflow and Peoples that have been helping me for years, here is my first question!
I have been programming in Android for some time, I like to test new ways to optimize apps. Now I came across Firebase Relatime Database and the possibility to trigger the entries on the server side with "function".I watched dozens of Firebase videos, rolled through the Firebase pages, and tried the whole thing with Typescript
Unfortunately my English is very, very bad and I can't find any examples of how I can get a simple code to work. I created a code with Node.js, tested it with firebase emulators: start, and it worked, but not on the Google server after "Firebase deploy". The goal is to make an entry in another path, after triggering an entry: I still don't understand the principle. that gives me the most difficulty, can someone explain what it could be and what it should look like?
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;
});
I also tried async, the result according to Console was the same
.onCreate(async (snapshot,context)=>{
......
await adaNameRef.child('test')
my Powershell Console says:
userId TZBAxFGSgZPZBoYZz7F4MVm5MAP2
> teamId -M797y0K4BBCEOa_nVoB
> spielerId -M7skpftMOOF4QlEW2kv
> original.name luigi
> Updates -M7skpftMOOF4QlEW2kv -M797y0K4BBCEOa_nVoB
i functions: Finished "setPlayerInTeam" in ~1s
> Synchronization succeeded
My Firebase Console Log says:
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 succes, no error?
Sorry, for the Bad English, everything is translated with google translated, I thank you already
Upvotes: 0
Views: 566
Reputation: 3789
I've created a sample code for you hopping this can help you to start with.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
//Initialize the App
admin.initializeApp(functions.config().firebase);
//When an userID is created, the function will be triggered
exports.makeUppercase = functions.database.ref('/pamyu/{userID}')
.onCreate((snapshot, context) => {
//Then I'll create an entry in this other path
//You can change the path according to your DB
admin.database().ref("/kyary").update({
userid: "1234567890"
});
});
Good luck! :D
Upvotes: 1