【问题标题】:Firebase cloud functions scheduledFunction gives Error: Process exited with code 16Firebase 云函数 scheduleFunction 给出错误:进程退出,代码 16
【发布时间】:2021-12-06 04:30:25
【问题描述】:

我正在尝试在 firebase 上运行计划的云功能。我在运行时在日志中看到此错误:

Error: Process exited with code 16
    at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:237:22)
    at process.emit (events.js:400:28)
    at process.emit (domain.js:470:12)
    at process.exit (internal/process/per_thread.js:169:15)
    at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:38:9)
    at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:233:22)
    at process.emit (events.js:400:28)
    at process.emit (domain.js:470:12)
    at processPromiseRejections (internal/process/promises.js:245:33)
    at processTicksAndRejections (internal/process/task_queues.js:96:32) 

这是我的功能:

exports.scheduledFunction = functions.pubsub
 .schedule("every 5 minutes")
 .onRun((context) => {
   console.log("This will be run every 5 minutes!");
   const tsToMillis = admin.firestore.Timestamp.now().toMillis();
   // const compareDate = new Date(tsToMillis - 24 * 60 * 60 * 1000);
   const compareDate = new Date(tsToMillis - 5 * 60 * 1000);
   admin
     .firestore()
     .collection("properties").where("status", "==", "Reserved")
     .where("reservationDate", "<", compareDate)
     .where("paymentIsDone", "==", false)
     .get()
     .then((querySnapshot) => {
       querySnapshot.forEach((doc) => {
         admin.firestore().collection("properties").doc(doc.id).update({
           agent: "",
           reservedBy: "",
           reservedByPhoneNumber: "",
           reservationDate: null,
           status: "Remaining",
         });
         console.log("canceling reservations");
       });
     });
   return null;
 });

我想在预订后 24 小时内(用于测试 5 分钟)内取消对未完成付款的住宿的预订。

这是我保留房产的方式:

exports.reserveApt = functions.https.onCall((data, context) => {
  if (!context.auth) {
    return {
      result: "failed",
      msg: "you are not authenticated",
    };
  }
  const doc = admin.firestore().collection("properties").doc(data.aptId);
  // get apt
  return doc.get().then((apt) => {
    apt = apt.data();
    // check apt is not already reserved or sold
    //   console.log(apt);
    if (apt.status !== "Remaining") {
      return {
        result: "failed",
        msg: "Appartment is already reserved or sold",
      };
    }
    // reserve apt with username and phone and changes status to reserved
    return doc
      .update({
        agent: data.agent,
        reservedBy: data.reserveeName,
        reservedByPhoneNumber: data.reserveePhone,
        reservationDate: admin.database.ServerValue.TIMESTAMP,
        status: "Reserved",
        paymentIsDone: false,
      })
      .then(() => {
        console.log("apt reserved");
        return {result: "success"};
      });
  });
});

是时间戳的问题还是什么?我不明白。

【问题讨论】:

    标签: node.js firebase cron google-cloud-functions timestamp


    【解决方案1】:

    您正在使用 .where() 在相等运算符(==array-contains)和不等运算符(&lt;&lt;=&gt;!=)之间进行复合查询,其中 @ 987654321@.

    admin
      .firestore()
      .collection("properties")
      .where("status", "==", "Reserved")  // First query, using equality comparator
      .where("reservationDate", "<", compareDate) // ERROR! You're using inequality operator '<' after '=='
    

    要执行此类操作,您需要create an index;这可以在following the error message 完成(检查您的Cloud Functions,单击您的函数,单击LOGS 选项卡并搜索错误)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多