I’m having trouble storing a value in the DB. Now it should be said, that I’ve saved something else under a separate key to the DB, and that worked fine; But for some reason, the function below just doesn’t get saved (I iterate over the keys to check if it exists, it’s never created).
func (m *Model) Save() error {
if m.context == editProject {
m.ProjectData.Name = m.projectForm.projectName.Value()
m.ProjectData.ContactName = m.projectForm.contactName.Value()
m.ProjectData.FaName = m.projectForm.faName.Value()
}
db, err := badger.Open(badger.DefaultOptions("./data"))
if err != nil {
logger.Errorf("Unable to open database.")
logger.Errorf(err.Error())
return err
}
defer db.Close()
// Since Badgerdb is a k/v store, we need to consider how we store
// structured data. So we use a identifiable key prefix "projects."
dbKey := fmt.Sprintf("project_%s\n", m.ProjectData.FaName)
// Save the project data
err = db.Update(func(txn *badger.Txn) error {
data, err := json.Marshal(&m.ProjectData)
if err = txn.Set([]byte(dbKey), data); err != nil {
fmt.Printf("Json Data: %s\n", data)
fmt.Printf("DB Set Error? %s\n", err)
return err
}
return nil
})
// return err
return nil
}
I’m new to Badger, and find it confusing why a different function, with a slightly different JSON structure (but still json) is saved, but the function above does not.