Challenge: Basic Node and Express - Use the .env File
Hi sorry to ask. I am struggling here. Even examples I have seen make no sense of this. It will not be capitalised but I have lost the thread of why MESSAGE_STYLES appears tagged on the back of process.env Link to the challenge:
āprocess.env.MESSAGE_STYLEā is a dot notation to refer to the enviroment variable āMESSAGE_STYLEā which is a property of the object āprocess.envā.
āprocess.envā holds all the values of environment variables as properties of the object, thus you need to refer to the variable with the dot notation āprocess.env.MESSAGE_STYLEā.
This reference to the variable is only for reading the value āuppercaseā in your script.
If you want to declare the variable and store the value āuppercaseā, do as follows:
if you are using replit you need to use secret tabs to store this variable.
If running project locally, store it in a ā.envā file.
Donāt store the variable by declaring let message=āuppercaseā, it doesnāt work for environment variables.
you can view the link provided by the hints here for the answer.
i am not able to find the problem can you help me here is my code :
const express = require(āexpressā);
const app = express();
const path = require(āpathā);
const mySecret = process.env.MESSAGE_STYLE;
When working with Replit, itās essential to be cautious about whitespace, especially when adding secrets. An unintended whitespace can lead to unexpected behavior or results.
For instance, consider these two scenarios:
Scenario 1: Extra space at the end of the value
{
"MESSAGE_STYLE": "uppercase "
}
Scenario 2: No extra space
{
"MESSAGE_STYLE": "uppercase"
}
The difference might seem trivial, but when your code relies on these exact values, such minor discrepancies can lead to significant issues.
Hereās a solution Iāve implemented which not only sends back the message but also the style itās using. This way, you can instantly check the messageStyle returned in the response and verify if the environment variable has any unintended whitespace:
I kind of test most of error to make some summary :
Set the variable āmessageā inside the function (outside get error)
Use the console.log( process.env.MESSAGE_STYLE) to see if correct (if you use replit, try this path to set env variable : (1.) ctrl + J (2) select āChange Tabā , find āSecretsā, add āNew Secretā (3) set āMESSAGE_STYLEā and āuppercaseā as key and value
Try to combine all of this chapter code to realize what are we doing here.