When looking at the NextJS MongoDB Starter, I noticed this bit of code:
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
global._mongoClientPromise = this.clientPromise;
}
Here is a link to the file I am talking about.
Now, I know of the global object, but what I do not understand is how merely assigning a value to it preserves it, since global._mongoClientPromise
is never accessed thereafter. In fact, this is the only line in the entire project where this property is used. Maybe it is obvious to some, but I am not a JavaScript expert, so can somebody please explain this to me?