To fix MongoNetworkError: failed to connect to server [localhost:27017] on first connect with Node MongoDB, we should make sure we’re connecting to a reachable Mongo server.
For instance, we write
await mongoose.connect("mongodb://localhost/testdb");
to call mongoose.connect
to connect to the testdb database on localhost.
The error shouldn’t be thrown if it’s reachable.