I have written a controller GetUser
to get a particular user from Database(Firestore) on the basis of id
I put in the query. If the user is not present in the database then it should give the message that "User not found"
. But along with this message I am getting nil
keyword also in response.
The response I am getting:
{
"message": "User not found"
}null
When I hover on dsnap.Data()
I get the information that
(firestore.DocumentSnapshot).Data on pkg.go.dev
Data returns the DocumentSnapshot's fields as a map. It is equivalent to
var m map[string]interface{}
d.DataTo(&m)
except that it returns nil if the document does not exist.
Controller:
func GetUser(c *gin.Context) {
paramID := c.Params.ByName("id")
........
........
........
dsnap, err := client.Collection("users").Doc(paramID).Get(ctx)
if err != nil {
fmt.Print(err)
c.IndentedJSON(http.StatusNotFound, gin.H{
"message": "User not found",
})
}
m := dsnap.Data()
c.IndentedJSON(http.StatusNotFound, gin.H(m))
}
Firestore reference link: https://pkg.go.dev/cloud.google.com/go/firestore@v1.6.1#DocumentSnapshot.Data
Can you guys please tell me how can I remove nil
from the response?
Thank you.
This problem is solved now. I write "return"
after the User not found
response.