我这样部署了云功能:

# Must call go vendor as go modules are ignored
go mod vendor
yes Y | gcloud functions deploy ${name} \
    --trigger-event providers/google.firebase.database/eventTypes/ref.create \
    --trigger-resource projects/_/instances/${databaseInstance}/refs/${referenceNode}/{nodeId} \
    --region ${region} \
    --runtime go113 \
    --allow-unauthenticated

从以下代码中,如何获取nodeId值?
// RTDBEvent is the payload of a RTDB event.
type RTDBEvent struct {
    Data  interface{} `json:"data"`
    Delta interface{} `json:"delta"`
}

// OnRealTimeDatabaseCreate prints information about a RealTime Database event.
func OnRealTimeDatabaseCreate(ctx context.Context, e RTDBEvent) error {
    // my code
}

谢谢你的帮助

最佳答案

为了从对象路径访问变量nodeID

尝试访问元数据值source

    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("%+v", e)
    return nil

关于go - 从Golang中创建的Cloud Function触发器引用获取实时数据库nodeID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62219905/

10-10 22:42