database - 在 Firestore 中更新值时没有字段错误

标签 database firebase go google-cloud-firestore

我正在尝试使用 golang 库更新 firestore 中的文档。出于某种原因,我收到一个错误:“没有字段\"BirthYear\” 错误,我不确定为什么。出生年份绝对是我尝试更新的值之一。

我假设我错误地配置了我的结构,但我看不出如何配置。这是我的结构和我的更新代码:

sharedstructs.Profile

type Profile struct {
    UID                string                `json:"UID" firestore:"UID"`
    ContactEmail       string                `json:"ContactEmail,omitempty" firestore:"ContactEmail"`
    BirthMonth         int64                 `json:"BirthMonth,omitempty" firestore:"BirthMonth"`
    BirthYear          int64                 `json:"BirthYear,omitempty" firestore:"BirthYear"`
    Gender             string                `json:"Gender,omitempty" firestore:"Gender"`
    Unit               string                `json:"Unit,omitempty" firestore:"Unit"`
    CurrentStatus      string                `json:"CurrentStatus,omitempty" firestore:"CurrentStatus"`
    Country            string                `json:"Country,omitempty" firestore:"Country"`
    ExperienceType     string                `json:"ExperienceType,omitempty" firestore:"ExperienceType"`
    DateJoined         time.Time             `json:"DateJoined,omitempty" firestore:"DateJoined"`
    Abilities          []Ability             `json:"Abilities,omitempty" firestore:"Abilities"`
    Goals              []Goal                `json:"Goals,omitempty" firestore:"Goals"`
    Roles              []Role                `json:"Roles,omitempty" firestore:"Roles"`
    TermsAndConditions []TermsAndConditions  `json:"TermsAndConditions,omitempty" firestore:"TermsAndConditions"`
    TimeZone           string                `json:"TimeZone,omitempty" firestore:"TimeZone"`
    BaselineTests      []BaselineTestResults `json:"BaselineTests,omitempty" firestore:"BaselineTests"`
    UpdatedDate        time.Time             `json:"UpdatedDate,omitempty" firestore:"UpdatedDate"`
    FirstName          *string               `json:"FirstName,omitempty" firestore:"FirstName"`
    LastName           string                `json:"LastName,omitempty" firestore:"LastName"`
    DisplayName        string                `json:"DisplayName,omitempty" firestore:"DisplayName"`
}

更新函数

func updateProfileWithSpecficValues(documentName string, values sharedstructs.Profile, overwriteValues []string) error {
    ctx := context.Background()
    app := firestorehelper.GetFirestoreApp()

    client, err := app.Firestore(ctx)
    if err != nil {
        return err
    }
    defer client.Close()

    //Set the updated date
    values.UpdatedDate = time.Now()
    wr, error := client.Doc(collectionName+"/"+documentName).Set(ctx, values, firestore.Merge(overwriteValues))
    if error != nil {
        return error
    }
    fmt.Println(wr.UpdateTime)
    //Assume success
    return nil
}

最佳答案

https://godoc.org/cloud.google.com/go/firestore#Merge

Merge returns a SetOption that causes only the given field paths to be overwritten. Other fields on the existing document will be untouched. It is an error if a provided field path does not refer to a value in the data passed to Set.

您没有在values 中发送BirthYear(默认值),但是在overwriteValues 中指定了BirthYear

关于database - 在 Firestore 中更新值时没有字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013105/

相关文章:

ios - 如何从Firebase Storage下载并声明大量图像?

javascript - 按与 Firestore 限制的不等式排序

go - 如何从 yaml 解码嵌入式结构

实现 Go goroutine 或 Go channel 的 C++ 库?

postgresql - "best practices"关于连接句柄重用和数据库用户设计是否相互排斥?

sql - 在 SQL (Postgresql) 中检查是否可以预订

sql-ex 错误的正确答案

mysql - 追随者/关注关系

javascript - 如何访问云功能内的Firebase数据库?

go - 云功能和本地依赖