I am using the inline editor for cloud functions in Google cloud platform. Runtime is Node.js 8. I want to access the firestore database. I found some code in some other forum. It's a cloud firestore on-create trigger.
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
const db = admin.firestore();
exports.myFunction = (data, context) => {
// access db here using something like db.collection('users');
}
I also read somewhere that, just calling admin.initializeApp() without parameters also works. And I can also move the 3 calls related to admin and db from outside the exports function to inside the exports function. So can anybody please tell me what is the right way to write the methods outside the exports functions? Also is there a way to access firestore db directly? Without admin initialize? I could not find much info about the inline editor so I am asking here. Please help.
My package.json
{
"name": "sample-firestore",
"version": "0.0.1",
"dependencies":
{
"semver": "^5.5.1",
"@google-cloud/firestore": "^1.3.0",
"firebase-admin": "^7.1.1"
}
}