如何在GO中通过Firestore设置云功能触发器

时间:2019-05-15 07:14:53

标签: go google-cloud-firestore google-cloud-functions

我正在尝试通过Firestore设置云功能触发器,仅使用Google的示例代码 https://cloud.google.com/functions/docs/calling/cloud-firestore

我只想为文档写入事件打印日志,而我只更改该函数的名称

我这样部署go文件

gcloud函数部署HelloLogger --runtime go111 --trigger-event provider / cloud.firestore / eventTypes / document.write --trigger-resource projects / [my project id] / databases / default / documents / post / {pushId }

数据库如下所示:https://imgur.com/3LSldnK

和功能页:https://imgur.com/vW5KiJX

我创建新文档并进行一些更新,购买我的HelloLogger而不记录任何消息

我的功能出了什么问题?

编辑:

休闲是我使用的示例代码,几乎没有变化

package logger

import (
        "context"
        "fmt"
        "log"
        "time"

        "cloud.google.com/go/functions/metadata"
)

// FirestoreEvent is the payload of a Firestore event.
type FirestoreEvent struct {
        OldValue   FirestoreValue `json:"oldValue"`
        Value      FirestoreValue `json:"value"`
        UpdateMask struct {
                FieldPaths []string `json:"fieldPaths"`
        } `json:"updateMask"`
}

// FirestoreValue holds Firestore fields.
type FirestoreValue struct {
        CreateTime time.Time `json:"createTime"`
        // Fields is the data for this value. The type depends on the format of your
        // database. Log the interface{} value and inspect the result to see a JSON
        // representation of your database fields.
        Fields     interface{} `json:"fields"`
        Name       string      `json:"name"`
        UpdateTime time.Time   `json:"updateTime"`
}

// HelloFirestore is triggered by a change to a Firestore document.
func HelloLogger(ctx context.Context, e FirestoreEvent) error {
        meta, err := metadata.FromContext(ctx)
        if err != nil {
                return fmt.Errorf("metadata.FromContext: %v", err)
        }
        log.Printf("Function triggered by change to: %v", meta.Resource)
        log.Printf("Old value: %+v", e.OldValue)
        log.Printf("New value: %+v", e.Value)
        return nil
}

0 个答案:

没有答案