Ufuk Sipor
Ufuk Sipor

Reputation: 17

firebase function error deploying a scheduled function listener

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

var database = admin.database();
//var tarih = new Date();
var d = Date.now(); 

var userTimeZoneOffset = 3; 

var timeInRegion = new Date(d + (userTimeZoneOffset*60*60*1000));

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('Turkey/Istanbul').onRun(async (context) => {
    var snapshot = await database.ref('Kullanicilar/{userId}/sistemdurumu').get();
    var oldDeger = snapshot.before.val();
    var newDeger = snapshot.after.val();
    
    if (newDeger > oldDeger){
      database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu1`).await().set(timeInRegion.getFullYear());
      database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu2`).await().set(timeInRegion.getMonth()+1);
      database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu3`).await().set(timeInRegion.getDate()); 
    }
  
    return null;
});

The error I get deploy. can you help me with this.

i deploying functions

functions: Finished running predeploy script. i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... functions: required API cloudbuild.googleapis.com is enabled functions: required API cloudfunctions.googleapis.com is enabled i functions: preparing functions directory for uploading... i functions: packaged functions (69.93 KB) for uploading i pubsub: ensuring required API pubsub.googleapis.com is enabled... i scheduler: ensuring required API cloudscheduler.googleapis.com is enabled... scheduler: required API cloudscheduler.googleapis.com is enabled pubsub: required API pubsub.googleapis.com is enabled functions: functions folder uploaded successfully i functions: updating Node.js 14 function scheduledFunctionCrontab(us-central1)... functions[scheduledFunctionCrontab(us-central1)]: Successful update operation. i functions: cleaning up build files... Functions deploy had errors with the following functions: scheduledFunctionCrontab(us-central1)

To try redeploying those functions, run: firebase deploy --only "functions:scheduledFunctionCrontab"

To continue deploying other features (such as database), run: firebase deploy --except functions

Error: Functions did not deploy properly.

Upvotes: 0

Views: 377

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50840

As mentioned in the documentation,

The value for timeZone must be a time zone name from the tz database.

Therefore the timezone should be Asia/Istanbul and not Turkey/Istanbul.

Apart from that, database update operations return a promise so you should use await. You can run them simultaneously in a Promise.all() if required.

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('Asia/Istanbul').onRun(async (context) => {
           ^^^^
    var snapshot = await database.ref('Kullanicilar/{userId}/sistemdurumu').get();
    var oldDeger = snapshot.before.val();
    var newDeger = snapshot.after.val();
    
    if (newDeger > oldDeger){
      await database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu1`).set(timeInRegion.getFullYear());
      await database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu2`).set(timeInRegion.getMonth()+1);
      await database.ref(`Kullanicilar/${Context.params.userId}/sistemdurumu3`).set(timeInRegion.getDate()); 
    }
  
    return null;
});

Upvotes: 2

Related Questions