#New to ts
25 messages · Page 1 of 1 (latest)
const URL = process.env.mongourl!;
if you're sure the environment variable is always there
it sometimes won't be there if the env file is not there, what do I do?
then the typescript error is correct
what should the app do when the env var is not there?
thonk
soo er do I need to handle the case where it might be undefined or leave it?
you probably should
ok ty
something like this good?
if (URL) {
const mongoClient = new MongoClient(URL, { useNewUrlParser: true, useUnifiedTopology: true });
} else {
throw new Error("Env file not configured properly. 'mongourl' not defined.")
}
not really
you can't access mongoclient outside the if that way
just do if (!URL) { throw the error }
ah yea sorry