getLastVisitsDetails does not return most recent data

In my website, I get the visitor id using Piwik.getTracker().getVisitorId(), which works as expected. I checked the calling URL and noted that the same value is also sent to Piwik in the _id field, so it should have recorded that call correctly.

The problem is that when I call getLastVisitsDetails, I cannot find the visitor id anywhere!
This sometimes works, and sometimes fails. I suspect getLastVisitsDetails is not actually returning the latest data but is caching it or reading from the archive, but I could be wrong. The most cases I encountered this were with first time visitors.
BTW the problem is not with limited records as getLastVisitsDetails is only returning 79 records while it can return up to 100

Please let me know if I am doing anything wrong and if there is a workaround.

For anyone else facing this issue, the solution is to add this to /config/config.ini:
[Tracker]
trust_visitors_cookies = 1

More details in the FAQ