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

The JSON parse drops the description but not title #1

Closed
hdale opened this issue Dec 13, 2020 · 8 comments
Closed

The JSON parse drops the description but not title #1

hdale opened this issue Dec 13, 2020 · 8 comments

Comments

@hdale
Copy link

hdale commented Dec 13, 2020

Hi! Thanks for a nice example of a full stack in Go!
But with this example code I have an issue. When sending JSON POST {"title":"some title","description":"some description"} I get an answer: {"error":"Title or Description not specified.","ok":false} . And the problem seems to be in the description part, because when I set in the ToDoController.go - > func CreateTodo() params.Description = "Hello" I get no error and the text will go to mongoDB.
I've tried to check of misspellings, but have not found any.
Any suggestions?

@jozsefsallai
Copy link
Owner

Hello!

That's kind of weird, I'll try to reproduce this issue.

I'd also like to mention that this repo and the guide associated to it were made using the first version of Fiber. I'd recommend switching to Fiber v2 as it brings lots of improvements to the framework.

@hdale
Copy link
Author

hdale commented Dec 13, 2020

Hi!
Thanks for your suggestion, but I did not succeed in the update. I added the v2 to all fiber imports and deleted the old go.sum and go.mod. Then ran the go mod init ...... Then I started the server and these errors poped up:
go run server.go
go: finding module for package go.mongodb.org/mongo-driver/mongo/options
go: finding module for package github.com/Kamva/mgm/v2
go: finding module for package go.mongodb.org/mongo-driver/bson
go: finding module for package github.com/gofiber/fiber/v2
go: found github.com/Kamva/mgm/v2 in github.com/Kamva/mgm/v2 v2.0.0
go: found github.com/gofiber/fiber/v2 in github.com/gofiber/fiber/v2 v2.2.5
go: found go.mongodb.org/mongo-driver/mongo/options in go.mongodb.org/mongo-driver v1.4.4

command-line-arguments

./server.go:36:10: cannot use controllers.GetAllTodos (type func(*fiber.Ctx)) as type func(*fiber.Ctx) error in argument to app.Get
./server.go:37:10: cannot use controllers.GetTodoByID (type func(*fiber.Ctx)) as type func(*fiber.Ctx) error in argument to app.Get
./server.go:38:11: cannot use controllers.CreateTodo (type func(*fiber.Ctx)) as type func(*fiber.Ctx) error in argument to app.Post
./server.go:39:12: cannot use controllers.ToggleTodoStatus (type func(*fiber.Ctx)) as type func(*fiber.Ctx) error in argument to app.Patch
./server.go:40:13: cannot use controllers.DeleteTodo (type func(*fiber.Ctx)) as type func(*fiber.Ctx) error in argument to app.Delete
./server.go:42:13: cannot use 3000 (type untyped int) as type string in argument to app.Listen

Notice I have added some comments to the files, so the original line numbers do not apply.

@hdale
Copy link
Author

hdale commented Dec 13, 2020

Hi! I think I find the breaking change in fiber v2:
gofiber/fiber#736
So the error parameter needs to be added.

@hdale
Copy link
Author

hdale commented Dec 13, 2020

Hi!
I added the error in each function where fiber-Ctx is used, but I get a bunch of new errors. It seems that the update from v1.4 to v2 has so many breaking changes that the sample code you gave needs several chnages. I'm just learning go, so I'm nit yet so comfortable with go that I can make those changes, so I would appreciate if you would update the sample code to handle the Fiber v2 changes. I think this example would give you good "points" in the future as Go and Fiber gets more new users.

@hdale
Copy link
Author

hdale commented Dec 14, 2020

I added the error return type and changed the return like this:
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{
"ok": true,
"todo": todo,
})
So it started to work after I also changed the app.Listen(3000) to app.Listen(":3000")
But still I'm left with the same problem that the description comes out the parser as nil.
Any suggestions?

@hdale
Copy link
Author

hdale commented Dec 14, 2020

I've changed the func CreateTodo()
func CreateTodo(ctx fiber.Ctx) error {
/

params := new(struct {
Title string
Description string
})
*/
type params struct {
Title string json:"title"
Description string json:"description"
Done bool json:"done"
}
var body params

err := ctx.BodyParser(&body)
if err != nil {
	return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{
		"error": "Cannot parse JSON",
	})
}
//ctx.BodyParser(&params)
if len(body.Title) == 0 || len(body.Description) == 0 {

	return ctx.Status(400).JSON(fiber.Map{
		"ok":    false,
		"error": "Title or Description not specified. Title: " + body.Title + " Descr.: " + body.Description + " . ",
	})
}

and my JSON POST
{
"title":"test todos 13",
"description:":"This is a test for todo 13"
}
recives again the same answer:
{
"error": "Title or Description not specified. Title: test todos 13 Descr.: . ",
"ok": false
}
And from the answer you can se that description is empty. and the parser does not give an error.
I just can't figure it out what is going on inside the ctx.BodyParser(&body) as it empties the description field ???.

@hdale
Copy link
Author

hdale commented Dec 14, 2020

Hi, Now I finally found the problem. I just was blind. I didn't notice my typo error in the sending JSON POST descrition key, I had there one semicolon too much.
Sorry for this confusion!
But now I have a working fiber v2 working!

@jozsefsallai
Copy link
Owner

glad it got fixed :) i should probably make another tutorial for fiberv2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants