ACF – Retrieve custom taxonomy from a relationship field

I managed to solve my issue by looping through a get_the_terms() function. In my case, I’ve also created a shortcode.

function display_taxonomy() { 

$prognoses = get_field('prognosis');
ob_start();
if( $prognoses ):
foreach( $prognoses as $prognosis ):
    $terms = get_the_terms( $prognosis->ID , 'my_custom_taxonomy' );
    foreach ( $terms as $term ):
        echo $term->name;
    endforeach;
endforeach;
endif;
return ob_get_clean();
}

add_shortcode( 'my_custom_taxonomy_shortcode', 'display_taxonomy' );