Uploaded image for project: 'Couchbase Go SDK'
  1. Couchbase Go SDK
  2. GOCBC-222

QueryResults.Next Returns False While .One Works Fine

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: User Error
    • None
    • None
    • None
    • None

    Description

      Hi,

      I'm having an issue with the developer version found here:

      github.com/couchbase/gocb

      The issue is with N1QL and reviewing the QueryResults that are returned. Take the following Golang snippet:

      ```

      var n1qlParams []interface{}
      n1qlParams = append(n1qlParams, context.Get(req, "pid").(string))
      query := gocb.NewN1qlQuery("SELECT `" + bucket.Name() + "`.* FROM `" + bucket.Name() + "` WHERE type = 'blog' AND pid = $1")
      rows, err := bucket.ExecuteN1qlQuery(query, n1qlParams)
      fmt.Println(query, n1qlParams)
      if err != nil {
      w.WriteHeader(401)
      w.Write([]byte(err.Error()))
      return
      }
      var row Blog
      var result []Blog
      /*rows.One(&row)
      fmt.Println(row)*/
      for rows.Next(&row) {
      fmt.Println(row)
      result = append(result, row)
      row = Blog{}
      }

      ```

      The first print that spits out the query and the N1QL parameters is accurate. The loop for the results is skipped because .Next is nil. However, if you uncomment the .One, it works fine. It is my understanding that .One calls .Next.

      Query monitoring using Couchbase log data determined that Couchbase is in fact receiving the correct query and the query is executing correctly. The problem is how Go is handling the results.

      Any more information needed, let me know.

      Thanks,

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          It turns out that the Go SDK was returning an error and it was being missed.  In the case of calling .One, the row was partially being parsed and then an error being returned, but the row was being printed in spite of this, giving the impression that .One was working.  In the case of .Next, some degree of error handling was implicitly included as the loop did not execute unless .Next returned true (which it was not).  The resolution was to inspect the returned error and then we could see that a type mismatch between the JSON and the Go structure was causing the parse to bail partway through the first row.

          brett19 Brett Lawson added a comment - It turns out that the Go SDK was returning an error and it was being missed.  In the case of calling .One, the row was partially being parsed and then an error being returned, but the row was being printed in spite of this, giving the impression that .One was working.  In the case of .Next, some degree of error handling was implicitly included as the loop did not execute unless .Next returned true (which it was not).  The resolution was to inspect the returned error and then we could see that a type mismatch between the JSON and the Go structure was causing the parse to bail partway through the first row.

          People

            brett19 Brett Lawson
            nic.raboy Nic Raboy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty