Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http response returns some empty fields on making a backend call to a database record with no empty fields. #41412

Closed
karasuno7 opened this issue Sep 15, 2020 · 1 comment

Comments

@karasuno7
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/adelicia/Library/Caches/go-build"
GOENV="/Users/adelicia/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/adelicia/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/adelicia/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1v/60bjrxr975b8_gm7rx8_0zyr0000gn/T/go-build688238576=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm making an HTTP request to the go backend server which fetches the record from mongodb atlas database.
Database contains just one record:
Screen Shot 2020-09-15 at 6 43 29 PM

GetProfile returns UserProfile user

func GetProfile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Methods", "GET")
params := mux.Vars(r)
var user models.UserProfile
fmt.Println("getProfile for : ", params["uid"])
filter := bson.M{"uid": params["uid"]}
err := collection.FindOne(context.Background(), filter).Decode(&user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte({ "message": " + err.Error() + " }))
return
// user = createUser(params["uid"])
}
json.NewEncoder(w).Encode(user)
}

router snippet

router := mux.NewRouter()
router.HandleFunc("/api/userProfile/{uid}", middleware.GetProfile).Methods("GET", "OPTIONS")

data-struct

type UserProfile struct {
ID primitive.ObjectID json:"_id" bson:"_id"
UserID string json:"uid" bson:"uid"
Pantry []string json:"pantry" bson:"pantry"
Cart []string json:"cart" bson:"cart"
Favourites []string json:"fav" bson:"fav"
}

What did you expect to see?

url in postman: http://localhost:8080/api/userProfile/aditit@andrew.cmu.edu

Screen Shot 2020-09-15 at 6 43 29 PM

What did you see instead?

Screen Shot 2020-09-15 at 6 52 08 PM

@davecheney
Copy link
Contributor

You have a mistake in your struct tags, go test will show you the problem.

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Sep 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants