I think I'm getting confused between firebase-admin
and Web SDK
I simply would like to connect a scheduled job that will push data into my Real-time database
Project structure
Package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
How can I correctly connect my scheduled cloud function to Firebase Database?
Here is my index.js where most scheduled functions will live
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
//const { push } = require("firebase/database")
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "myurl"
});
const database = admin.database();
exports.scheduledFunction = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => {
push(database, "/hello", {
message: "world"
})
return console.log("This will be run every 1 minutes!");
});
CodePudding user response:
The Admin SDK is not totally modular like the client SDK yet so you'll have to use the namespaced syntax if using the Admin SDK:
const db = admin.database();
exports.scheduledFunction = functions.pubsub
.schedule("every 1 minutes")
.onRun((context) => {
console.log("This will be run every 1 minutes!");
return db.ref('hello').push({ name: "Hellow" });
});