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