fix error handling of last record of forEach