csv.foreach and the insertOne calls are (presumably) both async, so you’re calling db.close() before your inserts have completed.
You need to come up with a way of waiting to call db.close() until all your inserts’ callbacks have been called. How to do that depends on how your csv module works, but using something like the async module can help with the async flow control.