【问题标题】:Write Json Webhook to Cloud Firestore with Cloud Functions. Cloud Function Failed to Deploy. Function failed on loading user code使用 Cloud Functions 将 Json Webhook 写入 Cloud Firestore。云功能部署失败。加载用户代码时函数失败
【发布时间】:2021-11-22 15:23:56
【问题描述】:

我有一个 Webhook,它可以将复杂的 JSON 负载传送到我的 Cloud Function URL,并将该 JSON 写入我的 Cloud Firestore 中的集合和文档。

我相信 Google Cloud Functions 上的 Node.JS 运行时使用 Express Middleware HTTP 框架。

我有一个 WooCommerce Webhook,希望我将 JSON 发送到 URL,我相信这是一个 POST http 请求。

我使用 Webhook.site 测试了 Webhook,它显示了正确的 JSON 负载。

开发人员建议我使用云函数来接收 JSON,解析 JSON 并将其写入 Cloud Firestore。

// cloud-function-name = wooCommerceWebhook
exports.wooCommerceWebhook = functions.https.onRequest(async (req, res) => {
    const payload = req.body;

    // Write to Firestore - People Collection
    await admin.firestore().collection("people").doc().set({
        people_EmailWork: payload.billing.email,
    });

// Write to Firestore - Volociti Collection
    await admin.firestore().collection("volociti").doc("fJHb1VBhzTbYmgilgTSh").collection("orders").doc("yzTBXvGja5KBZOEPKPtJ").collection("orders marketplace orders").doc().set({
        ordersintuit_CustomerIPAddress: payload.customer_ip_address,
    });

    // Write to Firestore - Companies Collection
    await admin.firestore().collection("companies").doc().set({
        company_AddressMainStreet: payload.billing.address_1,
    });
    return res.status(200).end();
});

如果有帮助,我有我的云功能部署失败的日志。

Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

我的 package.json:

{
  "name": "sample-http",
  "version": "0.0.1"
}

【问题讨论】:

  • 你能分享你的package.json文件吗?另外,您是否可以通过 CLI 输入 firebase functions:log 获得更多详细信息
  • 我已将 package.json 添加到原始帖子中。
  • 这是functions目录下的package.json文件吗??你至少应该在这个文件中找到"dependencies": { "firebase-admin": "^9.4.2", "firebase-functions": "^3.13.0", ... },
  • 这是通过 GCP UI 创建云函数时生成的默认 package.json。我可以添加这些依赖项并为您提供更新的错误消息(如果存在)。
  • 您不使用 Firebase CLI 进行部署吗?

标签: node.js json firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

Node.js需要正确define the dependencyFirebase Admin SDK,并初始化,如下图。

您还需要更改声明函数的方式:exports.wooCommerceWebhook = async (req, res) => {...} 而不是 exports.wooCommerceWebhook = functions.https.onRequest(async (req, res) => {...});。您使用的是通过 CLI 部署的 Cloud Functions。

package.json

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {    "firebase-admin": "^9.4.2"  }
}

index.js

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

exports.wooCommerceWebhook = async (req, res) => {  // SEE COMMENT BELOW
    const payload = req.body;

    // Write to Firestore - People Collection
    await admin.firestore().collection("people").doc().set({
      people_EmailWork: payload.billing.email,
    });

    // Write to Firestore - Volociti Collection
    await admin.firestore().collection("volociti").doc("fJHb1VBhzTbYmgilgTSh").collection("orders").doc("yzTBXvGja5KBZOEPKPtJ").collection("orders marketplace orders").doc().set({
      ordersintuit_CustomerIPAddress: payload.customer_ip_address,
    });

   // Write to Firestore - Companies Collection
   await admin.firestore().collection("companies").doc().set({
       company_AddressMainStreet: payload.billing.address_1,
   });

   return res.status(200).end();
 };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2020-11-03
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多