Fiber: 馃 ctx.JSON with empty slice

Created on 8 Sep 2020  路  3Comments  路  Source: gofiber/fiber

Question description

var founds []Product
err = db.Model(&founds).
    Where("status = ?", "on").
    Select()

// if founds = [], found nothing with status=on
c.JSON(founds)

If I make a postgres query, and make fiber response with empty slice.
The browser received with null, not the [] response.

WX20200908-150524

How can we make the fiber ctx.JSON with empty slice, and browser client received with []?

馃 Question

Most helpful comment

Please check this

// nil slice => null
var founds []Product
// empty slice => []
founds := []Product{}

All 3 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Please check this

// nil slice => null
var founds []Product
// empty slice => []
founds := []Product{}

If you don't have any further questions, I'll close this issue. Feel free to re-open it.

Was this page helpful?
0 / 5 - 0 ratings