Reputation: 1059
So i want to get all commPlans from IDs inside comms but for some reason i only get one object(which is the first ID in comms). Here is my code:
comms := models.GetComms(CommID)
if comms == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
var commPlans []models.CommPlan
for _, comm := range comms {
commPlans = models.GetCommPlans(comm.CommPlanID)
}
if commPlans == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
Upvotes: 0
Views: 730
Reputation: 38243
You need to append
the result from GetCommPlans
to the commPlans
slice, right now you're overwriting any previously returned result.
Either do:
comms := models.GetComms(CommID)
if comms == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
// a slice of slices
var commPlans [][]models.CommPlan
for _, comm := range comms {
commPlans = append(commPlans, models.GetCommPlans(comm.CommPlanID))
}
if commPlans == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
Or:
comms := models.GetComms(CommID)
if comms == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
var commPlans []models.CommPlan
for _, comm := range comms {
commPlans = append(commPlans, models.GetCommPlans(comm.CommPlanID)...)
}
if commPlans == nil {
componentsJson.WriteError(ctx, componentsError.ERROR_PARAMETERS_INVALID)
return
}
Upvotes: 3