简体   繁体   English

部署计划的函数侦听器的 firebase 函数错误

[英]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 功能:确保启用所需的 API cloudfunctions.googleapis.com... i 功能:确保启用所需的 API cloudbuild.googleapis.com... 功能:启用所需的 API cloudbuild.googleapis.com 功能:所需的 API cloudfunctions.googleapis .com 已启用 i 功能:准备用于上传的功能目录... i 功能:用于上传的打包功能 (69.93 KB) i pubsub:确保启用所需的 API pubsub.googleapis.com... i 调度程序:确保所需的 API 云调度程序。 googleapis.com 已启用...调度程序:所需的 API cloudcheduler.googleapis.com 已启用 pubsub:所需的 API pubsub.googleapis.com 已启用功能:功能文件夹上传成功 i 功能:更新 Node.js 14 功能 scheduleFunctionCrontab(us-central1 )... 函数[scheduledFunctionCrontab(us-central1)]:更新操作成功。 i functions: cleaning up build files... Functions deploy had errors with the following functions: scheduledFunctionCrontab(us-central1) i 函数:清理构建文件...函数部署有以下函数错误:scheduledFunctionCrontab(us-central1)

To try redeploying those functions, run: firebase deploy --only "functions:scheduledFunctionCrontab"要尝试重新部署这些功能,请运行: firebase deploy --only "functions:scheduledFunctionCrontab"

To continue deploying other features (such as database), run: firebase deploy --except functions要继续部署其他功能(例如数据库),请运行:firebase deploy --except functions

Error: Functions did not deploy properly.错误:函数未正确部署。

As mentioned in the documentation ,文档中所述,

The value for timeZone must be a time zone name from the tz database . timeZone 的值必须是tz 数据库中的时区名称。

Therefore the timezone should be Asia/Istanbul and not Turkey/Istanbul .因此时区应该是Asia/Istanbul而不是Turkey/Istanbul

Apart from that, database update operations return a promise so you should use await .除此之外,数据库更新操作返回一个承诺,所以你应该使用await You can run them simultaneously in a Promise.all() if required.如果需要,您可以在Promise.all()同时运行它们。

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM