create-go-app/fiber-go-template

GetBooks returns empty

dhinojosac opened this issue · 5 comments

I changed the method of sqlx to get all book in backend/app/queries/book_query.go:
err := q.Select(&books, "SELECT * FROM books")

Now, it's working!

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template!

koddr commented

Hi,

What do you mean, “it's working”? What kind of error or stack is output? To give more information, so far, it is not clear either the cause of the error or how to reproduce it to fix it (if it is confirmed, of course).

Sorry for the lack of information.
The function used to get all books in the template was the way to get a single result, for this way the return is always empty:

    // You can also get a single result, a la QueryRow
    jason = Person{}
    err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
    fmt.Printf("%#v\n", jason)

But since we want can to obtain the list of all books, we must use this other function (as it says in the documentation of jmoiron/sqlx):

// if you have null fields and use SELECT *, you must use sql.Null* in your struct
    places := []Place{}
    err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
    if err != nil {
        fmt.Println(err)
        return
    }

That's it! I hope it's useful!
Thanks

stale commented

👋 Hello. Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

stale commented

⚡️ This issue has been automatically closed because it has not had recent activity.