I am having an issue retrieving a JSON document that was created with no issue. The nested object within the JSON is empty, despite .Decode being used correctly.
func (r *CourseRepo) GetCourseById(ctx context.Context, id string) (Course, error) {
clog := log.GetLoggerFromContext(ctx)
var course Course
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return course, err
}
filter := bson.M{"_id": objID}
err = r.collection.FindOne(ctx, filter).Decode(&course)
if err != nil {
clog.ErrorCtx(err, log.Ctx{"msg": "an error occurred while finding a course"})
return course, err
}
return course, nil
}
Struct
type Course struct {
ObjectId primitive.ObjectID `bson:"_id, omitempty"`
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Lessons string `json:"lessons"`
Duration string `json:"duration"`
Details struct {
Title string `json:"title"`
Instructor string `json:"instructor"`
Introduction string `json:"introduction"`
Learn string `json:"learn"`
Topics string `json:"topics"`
Prerequisites string `json:"prerequisites"`
Goal string `json:"goal"`
AdditionalDetails string `json:"additionalDetails"`
HighLevelOverview string `json:"highLevelOverview"`
} `json:"course_details"`
}
I am having an issue retrieving a JSON document with an empty nested object. .Decode
should map the nested values, however, the resulting JSON is still empty.