【问题标题】:No Field Error Updating Values in Firestore在 Firestore 中更新值时没有字段错误
【发布时间】:2019-05-29 12:00:43
【问题描述】:

我正在尝试使用 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
}

【问题讨论】:

  • 您的代码看起来正确,您应该log.Printf("%v - %v", values, overwriteValues) 并检查结果

标签: database firebase go google-cloud-firestore


【解决方案1】:

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

Merge 返回一个仅导致给定字段路径的 SetOption 覆盖。现有文档上的其他字段将保持不变。 如果提供的字段路径未引用传递给 Set 的数据中的值,则会出错。

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

【讨论】:

  • 嗯,这是有道理的。我设置了一个断点并仔细检查了我的值,并且我的 overwriteValues 中有“BirthYear”。不知道为什么会抛出该错误
  • values中的每一项都没有默认值BirthYear?缺少它会导致错误。
【解决方案2】:

从 cloud.google.com/go/firestore v1.3.0 开始,当 valueStruct 是您可能读取或写入 Firestore 的完整结构时,我认为您无法通过 Set(..., valueStruct, firestore.Merge(sliceOfPaths)) 完成更新。

我收到一个错误字符串,包括 OP 引用的“无字段“[SomeFieldName]”字符串,但此错误中有更多信息。如果我的sliceOfPaths 引用了我的结构的两个元素的名称,比如一个 int 和一个 time.Time,我经常会收到一个错误,例如 'no field "[NameOfMyIntField] for value 2020-09-14T00:00:00Z "' 或反之亦然,它试图用整数更新我的 Firestore 文档的时间字段。

我只使用了Update() 而不是Set()。它有点笨拙,因为您必须传递 Update() 原始结构的特制子集(firestore.Update 的一部分),但它可以工作。

【讨论】:

  • 我遇到了完全相同的问题:我正在尝试使用带有合并选项的 Set 来更新 Firestore,但如果我提供了两个以上的路径,我总是会收到错误“没有字段 XXX 的值 YYY” .如何使用更新()?我试过了,但它似乎并不简单。我必须使用 protoreflect 来构建 firestore.Update 结构......但我现在不想试验反射......也许你能粘贴一个你如何使用 Update() 的例子 - 你也使用反思?
  • 我提出了一个单独的问题here
猜你喜欢
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多