For an AppInventor app which uses Firebase as database, I want to give unique user IDs to app users. After looking for some options I had the idea that I could use a Cloud HTTP function to be requested by the app, and the returning data would be a UserId
generated by simply incrementing the last UserId
in the UserIds
table (not sure if it is one, either).
That's the story, but I can't make the following code to deploy (and when I can, it doesn't work as I expect). It reads the last UserId
correctly, but I could only make it overwrite the previous data so far.
It throws an unexpected token error in the "."
in functions.https
.
const functions = require('firebase-functions');
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var LastUserId = function f() {return admin.database().ref('UserIds').limitToLast(1)}
exports.addWelcomeMessages = functions.https.onRequest((request, response)) => {
var ref = admin.database().ref('UserIds');
// this new, empty ref only exists locally
var newChildRef = ref.push();
// we can get its id using key()
console.log('my new shiny id is '+newChildRef.key());
// now it is appended at the end of data at the server
newChildRef.set({User : LastUserId + 1});
});