Search code examples
gofaunadb

How can I handle result value of Go version faunaDB API?


With Go version API of faunaDB, How can I handle the result of API?

I can get the document id which created by JS version API as ret.ref.value.id as follow:

client.query(
    q.Create(
        q.Collection(this.collection), 
          { data: JSON.parse(textarea_data) }))
.then( (ret) => {
  document_id = ret.ref.value.id;
}

But, With Go API, the Type of return value is map, but I can't handle as follows:

    ret, _ := client.Query(
        f.Create(
            f.Collection("label"),
            f.Obj{"data": f.Obj{"title": "What I had for breakfast .."}},
        ),
    )
    fmt.Println(ret)
    fmt.Println(ret["ref"])

The result of Println(ret) is as follows:

map[data:map[title:What I had for breakfast ..] ref:{257136925662511627 0x9fa1a0 0x9fa1a0 <nil>} ts:1581483731810000]

The result of Println(ret["ref"]) is error as follows:

invalid operation: ret["ref"] (type faunadb.Value does not support indexing)

How can I get the created document id? Thank you for your suggestion!


Solution

  • I can get it as follows:

        var ref f.RefV
    
        ret, _ := client.Query(
            f.Create(
                f.Collection("label"),
                f.Obj{"data": f.Obj{"title": "What I had for breakfast .."}},
            ),
        )
    
        if err := ret.At(f.ObjKey("ref")).Get(&ref); err == nil {
            fmt.Println(ref.ID)
        }
    

    The key points are Value.At(), ObjKey(), Get(), and RefV