The string | argument undefined" cannot be assigned to the string parameter.
The undefined type cannot be assigned to the string type.ts(2345)
I can fix this error with setting an exclemation mark behind the Mongo_url like this: const conn = await mongoose.connect(process.env.MONGO_URL!);
But then the console tells me:
TypeError: Cannot read property ‘underline’ of undefined
When I log my MONGO_URL it gives me the correct connection path.
Can someone help me out? Thanks
Thanks for your answer kevinSmith. I have tested this and the console throws me no error ‘mongo url not defined’. But the app still crushes with the error:
cannot read property ‘underline’ of undefined
So I think conn.connection.host must be undefined.
Isn’t it telling you that it can be undefined and that isn’t assignable to a string?
Argument of type ‘string | undefined’ is not assignable to parameter of type ‘string’.
I would check it exists before using it.
There isn’t really any useful fall-back value. Falling back on an empty string might satisfy TS, but it doesn’t really help you if the environment variable is undefined. Check the variable exists before using it, you might also want try/catch the connect as well.