【问题标题】:Firebase cloud function not doing anything with XstateFirebase 云功能对 Xstate 没有任何作用
【发布时间】:2020-06-19 06:26:03
【问题描述】:

我正在尝试在 Firebase 上编写一个云函数,该函数会在编写另一个文档时更新 Firebase 中的文档。为此,我使用了触发方法 onWrite。我为此使用 Xstate,因为我的原始代码更复杂,但想法是相同的(问题也是如此)。这是我使用的代码(打字稿):

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { Machine, interpret } from "xstate";

admin.initializeApp({});

// This function will trigger as soon as a product of a company owner gets updated.
exports.productsOnUpdate = functions.firestore
  .document(`companies/{companyId}/{products}/{productId}`)
  .onWrite((change: any) => {

    let done = false;

    const PromiseFunction = (docIn: any) => {
      console.log(docIn);
      return admin
        .firestore()
        .collection("test")
        .doc("testdoc")
        .set({ products: docIn.products }, { merge: true })
        .then((doc: FirebaseFirestore.WriteResult) => doc.writeTime);
    };

    const myMachine = Machine<any>({
      id: "myMachine",
      initial: "idle",
      context: { doc: { products: "a product" } },
      states: {
        idle: {
          on: {
            INIT: "init"
          }
        },
        init: {
          invoke: {
            id: "setDoc",
            src: (context, event) => PromiseFunction(context.doc),
            onDone: {
              target: "success"
            },
            onError: {
              target: "failure"
            }
          }
        },

        success: {},
        failure: {}
      }
    }); // end of machine

    const MyMachine = interpret(myMachine).onTransition(state => {
      console.log(state.value);
      // actually do something here
      switch (state.value) {
        case "INIT":
          break;

        case "success":
          done = true;
          console.log("Success");

        case "failure":
          console.log("Something went wrong");

        default:
          break;
      }
    });

    MyMachine.start();
    MyMachine.send("INIT");
    while (done === false);
    return "ok";
  });

因此,当尝试更新子集合“产品”中的文档时,应该会触发该功能。在日志中,我看到以下内容:

绝对没有任何反应。当我在 MyMachine 的上下文中犯了一个小错误时(将 context: { doc: { products: "a product" } } 更改为 context: { doc: { product: "a product" } }, 我确实看到了:

因此,承诺处理似乎有问题。我已经花了一天的时间;任何帮助表示赞赏!

【问题讨论】:

  • console.log(docIn); 的结果是什么?
  • 它甚至不执行该语句,所以这就是它没有调试行的原因。
  • 那么问题出在你的machine,你的代码也因为while (done === false);而发臭
  • 我使用while(done===false) 的原因是因为云函数似乎已经在状态机仍在运行时返回“ok”(又名完成)。有什么不同的建议吗?
  • 您可以在工作完成时返回承诺并解决承诺

标签: javascript firebase google-cloud-functions xstate


【解决方案1】:

您应该让xstate 解决您的承诺。从您的PromiseFunction 中删除then 语句:

const PromiseFunction = (docIn: any) => {
  console.log(docIn);
  return admin
    .firestore()
    .collection("test")
    .doc("testdoc")
    .set({ products: docIn.products }, { merge: true })
};

onDone 块中处理已解决的Promise

onDone: {
  target: "success",
  actions: (ctx, e) => console.log(e), // do stuff with your resolved Promise
}

【讨论】:

  • 出现了两件事: - Firebase 根本没有正确执行该函数。更改函数名称会产生不同的结果。 - 你的回答确实让它以某种方式工作。谢谢!
  • @PaulusPotter,完美!我很高兴它对你有帮助 =)
猜你喜欢
  • 2021-01-12
  • 1970-01-01
  • 2018-10-16
  • 2022-03-29
  • 2020-06-09
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2019-10-09
相关资源
最近更新 更多