0

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
}

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh i see...Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.