You are not awaiting in the initial for loop so setRouteHeader is called multiple times simultaneously. This causes the find and update operations to overlap causing what you have observed here. To fix it wrap the for loop in an asynchronous function and add await before calling setRouteHeader then call this asynchronous function