Iterate ExecuteN1qlQuery value result in panic

hi, this is my first time using Couchbase, i’m trying basic auth and CRUD, idk but gocb return panic and exit

package main

import (
	"fmt"
	"gopkg.in/couchbase/gocb.v1"
)

type User struct {
	Id string `json:"uid"`
	Email string `json:"email"`
	Interests []string `json:"interests"`
}

func main() {
        cluster, _ := gocb.Connect("couchbase://192.168.1.192")
        cluster.Authenticate(gocb.PasswordAuthenticator{
            Username: "clusterUsername",
            Password: "clusterPassword",
        })
        bucket, _ := cluster.OpenBucket("bucket_name", "")

        fmt.Println("aman")

        // bucket.Manager("", "").CreatePrimaryIndex("", true, false)

        bucket.Upsert("u:kingarthur",
                User{
                        Id: "kingarthur",
                        Email: "kingarthur@couchbase.com",
                        Interests: []string{"Holy Grail", "African Swallows"},
                }, 0)

        fmt.Println("amaf32n")

        // Get the value back
        var inUser User
        bucket.Get("u:kingarthur", &inUser)
        fmt.Printf("User: %v\n", inUser)

        fmt.Println("amanf3a")

        // Use query
        query := gocb.NewN1qlQuery("SELECT * FROM bucket_name WHERE $1 IN interests")
        rows, _ := bucket.ExecuteN1qlQuery(query, []interface{}{"African Swallows"})
        var row interface{}
        for rows.Next(&row) {
                fmt.Printf("Row: %v", row)
        }
}

go run :

aman
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x6c92da]

goroutine 1 [running]:
gopkg.in/couchbase/gocb%2ev1.(*Bucket).startKvOpTrace(...)
	/home/grandong/go/src/gopkg.in/couchbase/gocb.v1/bucket.go:38
gopkg.in/couchbase/gocb%2ev1.(*Bucket).Upsert(0x0, 0x78db06, 0xc, 0x74e6a0, 0xc00005c5c0, 0x0, 0x0, 0x0, 0x0)
	/home/grandong/go/src/gopkg.in/couchbase/gocb.v1/bucket_crud.go:75 +0x7a
main.main()
	/home/grandong/Documents/Development/test/go-couchdb/main.go:26 +0x28a
exit status 2

go version go1.12.5 linux/amd64

Hey @jihantoro,

Based on the error message we can see above, it looks like your OpenBucket call is actually failing and thus your calling a method against a nil object. Try capturing the errors and printing them to see whats going on.

        cluster, cerr := gocb.Connect("couchbase://192.168.1.192")
        if cerr != nil {
          fmt.Printf("Cluster Open Error: %v", cerr)
        }

        cluster.Authenticate(gocb.PasswordAuthenticator{
            Username: "clusterUsername",
            Password: "clusterPassword",
        })
        bucket, berr := cluster.OpenBucket("bucket_name", "")
        if berr != nil {
          fmt.Printf("Bucket Open Error: %v", berr)
        }

Cheers, Brett

1 Like

actually, the gocb.Connect is working fine, but ,

package main

import (
	"fmt"
	"gopkg.in/couchbase/gocb.v1"
)

type User struct {
	Id string `json:"uid"`
	Email string `json:"email"`
	Interests []string `json:"interests"`
}

func main() {
        cluster, _ := gocb.Connect("couchbase://192.168.1.192")
        cluster.Authenticate(gocb.PasswordAuthenticator{
            Username: "username",
            Password: "password",
        })
        bucket, _ := cluster.OpenBucket("bucket_name", "")

        bucket.Insert("u:kingarthur",
                User{
                        Id: "kingarthur",
                        Email: "kingarthur@couchbase.com",
                        Interests: []string{"Holy Grail", "African Swallows"},
                }, 0)
        // success insert
        fmt.Println("success insert")

        // Get the value back
        var inUser User
        bucket.Get("u:kingarthur", &inUser)
        fmt.Printf("User: %v\n", inUser.Email)
        // success get

        myQuery := gocb.NewN1qlQuery("select * from bucket_name")
        rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)

        var GetUser User
        // start failing
        for rows.Next(&GetUser) {
            fmt.Println(GetUser.Email)
        }

        if err = rows.Close(); err != nil {
            print(err)
        }

}

error log :

success insert                                                                      
User: kingarthur@couchbase.com                                                      
panic: runtime error: invalid memory address or nil pointer dereference             
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x6d59be]             
                                                                                    
goroutine 1 [running]:                                                              
main.main()                                                                         
        /home/fullpath/main.go:44 +0x55e      
exit status 2

my code start failing when call : for rows.Next(&GetUser) {

right

caused by :
[4000] No index available on keyspace bucket_name that matches your query. Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online.