I have a function in nodejs10 with event trigger (ref.create) on a specific path, it works because function triggers on this path normally.
Problem is, I get this error in log, and I can't solve it:
SyntaxError: Unexpected token . in JSON at position 0 at JSON.parse (<anonymous>) at Object.firebaseConfig (/workspace/node_modules/firebase-functions/lib/config.js:45:21) at resourceGetter (/workspace/node_modules/firebase-functions/lib/providers/database.js:116:38) at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:116:13)
This is my index.js
const admin = require('firebase-admin');
var serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://***.firebaseio.com"
});
exports.myFunction = admin.database().ref(<PATH>).onCreate(async (snap, context) => {.....
I'm deploying it based on this documentation: https://cloud.google.com/functions/docs/calling/realtime-database#deploying_your_function
and also with envars FIREBASE_CONFIG and GCLOUD_PROJECT
Since you are already using the Firebase SDK, deploying with gcloud
is fine, but I suggest that you deploy your functions using the Firebase CLI.
To explain the error on your code, the correct way to create a function with a Real Time Database event is with functions.database
. Here's an example:
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp();
exports.rtdbOncreate = functions.database.ref('/foo/{bar}').onCreate(async (snap, context) => {
... // your code logic
})
If you're deploying an app to Cloud Functions, I wouldn't recommend uploading a service account as other people who have access to your project might see it, once they view your function's source code in the console.
For additional reference, see getting started with Firebase Cloud Functions.