1

I have deployed a cloud function as bellow

package functions

import (
    "context"
    "encoding/json"
    "log"

    "github.com/Capstone/models"
    "github.com/GoogleCloudPlatform/functions-framework-go/functions"
    "github.com/cloudevents/sdk-go/v2/event"
)

func init() {
    // Register a CloudEvent function with the Functions Framework
    functions.CloudEvent("StoreAuditRecords", StoreAuditRecords)
}

func StoreAuditRecords(ctx context.Context, m event.Event) error {

    // Extract data from CloudEvent
    data := m.Data()

    // Unmarshal the decoded data into the AuditRecord struct
    var auditRecord models.AuditRecord
    if err := json.Unmarshal(data, &auditRecord); err != nil {
        log.Printf("Error unmarshaling data: %v", err)
        return err
    }

    // Store data to Firestore
    _, _, err := firestoreClient.Collection("audit-records").Add(ctx, auditRecord)
    if err != nil {
        log.Printf("Error storing data to Firestore: %v", err)
        return err
    }

    return nil
}

It processes the pub/sub message and store it into a collection named "audit-records". All the clients are initialized appropriately.

Note that - I have a push subscription to which this function's endpoint url is linked to with the Push payload unwrapping enabled.

But I am facing an error that is

unable to extract background event from {"Action":"Update","DocumentId":"P3y4uItPTGR1ACdj8vCx","Timestamp":"2024-01-29 05:48:31","UserId":"[email protected]"}

Where {"Action":"Update","DocumentId":"P3y4uItPTGR1ACdj8vCx","Timestamp":"2024-01-29 05:48:31","UserId":"[email protected]"} is the message data being published to pubsub topic to which it subscribe.

I seek help.

I want to store the message payload data to my collection as in code but I am getting the error above.

4
  • How your push subscription is configurer? Did you use the "no wrapper" option? Commented Jan 29, 2024 at 8:13
  • 1
    Thanks Guillaume for your comment. I made a , silly mistake. It was my bad. I realized that and corrected it. Commented Jan 29, 2024 at 10:04
  • It was the issue? If so, it's interesting, and I will perform a feedback to Google Cloud. Commented Jan 29, 2024 at 12:31
  • No there wasn't any issue. I was making a tiny mistake by giving wrong project ID in my variable while making firestore client. Commented Jan 30, 2024 at 0:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.