I am trying to working from this post How can I have dynamic properties in go on the google app engine datastore

Unable to insert the data to the datastore, It is only creating ID

import (
    "log"
    "net/http"
    "time"

    "github.com/julienschmidt/httprouter"
    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
)

type DynEnt map[string]interface{}

func (d *DynEnt) Load(props []datastore.Property) error {
    // Note: you might want to clear current values from the map or create a new map
    for _, p := range props {
        (*d)[p.Name] = p.Value
    }
    return nil
}

func (d *DynEnt) Save() (props []datastore.Property, err error) {
    for k, v := range *d {
        props = append(props, datastore.Property{Name: k, Value: v})
    }
    return nil, nil
}

//GetAccountHandler is to
func GetAccountHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

    c := appengine.NewContext(r)

    d := DynEnt{"email": "[email protected]", "time": time.Now()}
    log.Println("d=>", d)
    log.Println("&d=>", &d)
    k := datastore.NewIncompleteKey(c, "DynEntity", nil)
    key, err := datastore.Put(c, k, &d)
    log.Fatalf("%v %v", key, err)

}

1 Answers

2
icza On Best Solutions

There was a typo in my original answer, the return value at the end of Save() was a copy/paste mistake (from the channel version). The correct version is:

func (d *DynEnt) Save() (props []datastore.Property, err error) {
    for k, v := range *d {
        props = append(props, datastore.Property{Name: k, Value: v})
    }
    return
}

Note that there are no values specified after return because named result parameters are used. Specifying nil, nil would not return the slice built inside Save().

Also you call log.Fatalf() at the end of your handler, that terminates your app. Try log.Printf() instead.