Skip to content

For some reason the data is not showing.  #1532

@nanduGiyer

Description

@nanduGiyer

Issue description

Everything works, but the JSON is not showing any results. I had three records where the query iterated three times, but the result was empty.

Example code

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"sync"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"

	"github.com/gorilla/mux"

	"raftnandu/services/lib/common"
	"raftnandu/services/lib/config"
	"raftnandu/services/lib/constant"
)

type UserResult struct {
	ID            int
	UserName      string
	UserType      string
	Isactive      int
	Createdat     time.Time
	Createdby     int
	Modifiedby    int
	Modfiedat     time.Time
	Permissionid  int
	Lastlogintime time.Time
	Role          string
	Emailid       string
	Description   string
}

var ctx = common.AppContext{}
var dbcon *sqlx.DB

func main() {
	config.LoadConfigurations()
	
	ctx.DeferredWG = &sync.WaitGroup{}

	
	router := mux.NewRouter()

	router.HandleFunc("/user", GetUsers).Methods("GET")
	
	http.ListenAndServe(":7000", router)

}

func GetUsers(w http.ResponseWriter, r *http.Request) {
	db := getMySQLDB()
	getQuery := `select  usr.id as id, 
			usr.username as username, 
			ut.name as usertype, 
			usr.isactive as isactive,
			usr.createdby as createdby, 
			usr.modifiedby as modifiedby,
			usr.permissionid as permissionid,
			usr.lastlogintime as lastlogintime,
			rol.name as role,
			ui.emailid as emailid,
			ui.description as description
		from user usr
		join userinfo ui on ui.userid =usr.id
		join usertype ut on ut.id = usr.type
		join user2role u2r on usr.id = u2r.userid 
		join role rol on rol.id = u2r.roleid`

	rows, err := db.Query(getQuery)
	usrinfo := []UserResult{}
	usr := UserResult{}
	if err != nil {
		fmt.Fprintf(w, "Query Error: "+err.Error())
	} else {
		for rows.Next() {
			rows.Scan(&usr.ID, &usr.UserName, &usr.UserType, &usr.Isactive, &usr.Createdat,
				&usr.Createdby, &usr.Modfiedat, &usr.Modifiedby, &usr.Description, &usr.Role,
				&usr.Permissionid, &usr.Emailid, &usr.Lastlogintime)
			usrinfo = append(usrinfo, usr)			
		}
		json.NewEncoder(w).Encode(usrinfo)
	}

}

func getMySQLDB() *sqlx.DB {
	var err error
	db_type := config.GetEnv(config.DB_TYPE)
	db_port := config.GetEnv(config.DB_PORT)
	db_server := config.GetEnv(config.DB_ADDRESS)
	db_user := config.GetEnv(config.DB_USER)
	db_password := config.GetEnv(config.DB_PASSWORD)
	db_name := config.GetEnv(config.DB_NAME)

	connString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
		db_user, db_password, db_server, db_port, db_name)

	dbcon, err = sqlx.Open(db_type, connString)
	if err != nil {
		log.Fatal("Error connecting to Database connection pool:" + err.Error())
	}
	return dbcon
}

Result:

[
    {
        "ID": 0,
        "UserName": "",
        "UserType": "",
        "Isactive": 0,
        "Createdat": "0001-01-01T00:00:00Z",
        "Createdby": 0,
        "Modifiedby": 0,
        "Modfiedat": "0001-01-01T00:00:00Z",
        "Permissionid": 0,
        "Lastlogintime": "0001-01-01T00:00:00Z",
        "Role": "",
        "Emailid": "",
        "Description": ""
    },
    {
        "ID": 0,
        "UserName": "",
        "UserType": "",
        "Isactive": 0,
        "Createdat": "0001-01-01T00:00:00Z",
        "Createdby": 0,
        "Modifiedby": 0,
        "Modfiedat": "0001-01-01T00:00:00Z",
        "Permissionid": 0,
        "Lastlogintime": "0001-01-01T00:00:00Z",
        "Role": "",
        "Emailid": "",
        "Description": ""
    },
    {
        "ID": 0,
        "UserName": "",
        "UserType": "",
        "Isactive": 0,
        "Createdat": "0001-01-01T00:00:00Z",
        "Createdby": 0,
        "Modifiedby": 0,
        "Modfiedat": "0001-01-01T00:00:00Z",
        "Permissionid": 0,
        "Lastlogintime": "0001-01-01T00:00:00Z",
        "Role": "",
        "Emailid": "",
        "Description": ""
    }
]

Error log

If you have an error log, please paste it here.

Configuration

Driver version (or git SHA):

Go version: run go version in your console

Server version: E.g. MySQL 5.6, MariaDB 10.0.20

Server OS: E.g. Debian 8.1 (Jessie), Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions