简体   繁体   中英

Aerospike Golang client putObject method gives me panic: reflect: call of reflect.Value.Elem on map Value

I'm trying to save a the results from MongoDB into AeroSpike. I'm using the MGO client for Mongodb. The code is the following :

package main

import (
    "log"
    "flag"
    "fmt"
    ///"reflect"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    as "github.com/aerospike/aerospike-client-go"
   /// "encoding/json"
)


/*
Iterating through the results
 */
 results := make(map[string]interface{})
 iter := c.Find(nil).Iter()
for iter.Next(&results) {

    tmp := make(map[string]interface{})
    b, _ := bson.Marshal(results)
    bson.Unmarshal(b, &tmp)
        log.Println("func (interface, interface):",  tmp["_id"])
/*
Aerospike Key
 */
        key, err := as.NewKey(*Namespace, *Set, "LIST")
if err != nil {
    log.Fatal(err)
}

/*
Saving to aerospike
 */
client.PutObject(WritePolicy, key, tmp)

}
if err := iter.Close(); err != nil {
   fmt.Println(err)
}

I get the following error from Aerospike :

panic: reflect: call of reflect.Value.Elem on map Value

goroutine 1 [running]:
reflect.Value.Elem(0x24dd40, 0xc20803b020, 0x15, 0x0, 0x0, 0x0)
    /usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:703 +0x1d5
github.com/aerospike/aerospike-client-go.marshal(0x24dd40, 0xc20803b020, 0xc208052101, 0x0, 0x0, 0x0)
    /Users/milos/Downloads/golang/src/github.com/aerospike/aerospike-client-go/marshal.go:143 +0xa1

I have turned to google and had no luck with this.

For a sanity test I have ran the following to see if the struct saves into AeroSpike and it does save.

 type OBJECT struct {
    Price  int
    DBName string
}

obj := &OBJECT{198, "Jack Shaftoe and Company"}
 errr := client.PutObject(WritePolicy, key, obj)
if errr != nil {
    log.Fatal(errr)
}
}
if err := iter.Close(); err != nil {
   fmt.Println(err)
}

You are trying to save a map into aerospike as oppose to a struct .

I think that your best option is to encapsulate the map inside a struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM