Use the .env File -- challenge is not accepting solution

Tell us what’s happening: Describe your issue in detail here.

My issue is that I’ve written this code several different (correct) ways in javascript and the challenge is not recognizing the correctness of the code. Maybe I am missing something here, but the .env file feature is deprecated in Repl. There is no way to include it in the file system. The SECRETS tab has the environment variable features, which I have used and tested that it logs correctly that it works in the console, but I’m unable to confirm it within the FCC challenge itself.

Your project link(s)

solution: https://replit.com/@emilypmendez/boilerplate-express

require('dotenv').config();
var express = require('express');
var app = express();
const mySecret = process.env['MESSAGE_STYLE'];

app.use('/public', express.static(__dirname + '/public'));

app.get('/', function(req, res){
  console.log("Hello world");
  res.sendFile(__dirname + '/views/index.html');
});

console.log('Begin JSON get method');

app.get('/json', (req, res) => {
  if (process.env.MESSAGE_STYLE==='uppercase') {
    return res.json({"message": "Hello json"}).toUpperCase();
  } else {
    return res.json({"message": "Hello json"});
  }
});

console.log('End JSON get method');

// app.get("/json", (req, res) => {
//   let message = {"message": "Hello json"};
  
//   if( process.env.MESSAGE_STYLE === 'uppercase'){
//       return message['message'] = message['message'].toUpperCase();
//       console.log(message['message']);
//   } else {
//       return res.json(message);
//   }
// });


 module.exports = app;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Use the .env File

Link to the challenge:

I don’t think this does what you think this does.

Yup:

Note: If you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.

Hi Jeremy, thanks for posting. I’m not really sure what you mean by your comment. Could you please elaborate?

What exactly do you want to make uppercase? You can’t stick in on the entire JSON.

I’m looking to essentially make the “Hello json” message all uppercase upon the condition that the environment variable exists and otherwise it would print the regular JSON message. I’ve written this in a multitude of different ways (please refer to the commented-out piece of code) but can’t seem to get it to fully function for the sake of the project. Do you have any clue as to what I might be doing wrong here? Any guidance will help. Thanks

This is the only thing that should be uppercase. Only that. Move the toUpperCase

Do you mean it should be written in this manner? I wrote two versions below. Neither of these attempts work though.

app.get('/json', (req, res) => {
  let message = {"message": "Hello json"}
  if (process.env.MESSAGE_STYLE==='uppercase') {
    return message.toUpperCase();
  } else {
    return res.json(message);
  }
});
app.get('/json',(req,res)=>{
  if(process.env.MESSAGE_STYLE==="uppercase"){
    return res.json({
    "message":"HELLO JSON"
    })
  }else{
    return res.json({
      "message":"Hello json"
    })
  }
})

Absolutely nowhere have you written HELLO JSON or "Hello json".toUpperCase(). (Edit, missed the second, but you shouldn’t need a return there) That string is the only thing that should be uppercase.

I understand that string is the only thing that should be uppercase. I wrote HELLO JSON in the second code block above. If you are aware of it, what exactly am I doing wrong? I would like to learn from my mistake… thanks

The last code you posted will pass, but you do not have that code in your Replit.

I do have that 2nd piece of code in the Replit… and have tried to complete the challenge with that code, but it does not accept the solution. I’m not sure what else to try :- (

Current Code:

// This shouldn't be here, but it appears to be harmless
require('dotenv').config();

var express = require('express');
var app = express();

// This shouldn't be here, harmless but confusing
const mySecret = process.env['MESSAGE_STYLE'];

app.use('/public', express.static(__dirname + '/public'));

app.get('/', function(req, res) {
  console.log("Hello world");
  res.sendFile(__dirname + '/views/index.html');
});

app.get('/json', (req,res) => {
  if (process.env.MESSAGE_STYLE === "uppercase") {
    // You shouldn't need 'return' here but is harmless
    return res.json({
    "message":"HELLO JSON"
    })
  } else {
    // Same comment about return
    return res.json({
      "message":"Hello json"
    })
  }
});

 module.exports = app;

When I run this code in my repl, the tests pass

The route code is correct, but if you go to the /json route you can see it sends the lowercase version which it wouldn’t do if you have set up the environment variable correctly.

The key and value should not be quoted, so make sure you add uppercase and not "uppercase" as the value.

If I clone your replit and add the key and value it passes the test.

OK, thank you Jeremy. I’ve adjusted the code snippet in my Replit to reflect your comments but it’s still not working. I suspect the need to create a new Replit repo for this particular challenge. I’ve been stuck on this for two days now without resolution, the only thing I haven’t tried is attempting to create a new repo. I will report back shortly!

I’d double check your SECRETS tab.
image

Yes that’s exactly what my SECRETS tab looks like. That was why I was confused why it wasn’t working in the original repo… created a new Replit repo with the proposed correct solution and a SECRET tab as well, still not working. I’m really not sure what to do. This is the new repo:
https://boilerplate-express-1.emilypmendez.repl.co

Not sure where the quotes went around "uppercase"?

  if(process.env.MESSAGE_STYLE==uppercase){

Thanks. Trying your suggestion of using uppercase without the string ticks… not sure this value is working either. Attached a screencap of what I’ve got on a new repo, but it’s not passing the challenge. :confused:

https://boilerplate-express-1.emilypmendez.repl.co

If you strip the quotes there, you are referring to a variable called uppercase that is not actually defined. Inside of the SECRETS is where you shouldn’t have the quotes.

I had the wrong picture before:
image

This is what you should see in the ‘Raw Editor’ for the secrets.

1 Like

Okay that is new! Thanks. I tried this update within the SECRETS but it ultimately wasn’t passing the test because I had to restart the console server and then try passing the test again. I was feeling really quite stumped on this challenge, thanks for all your help! Happy new year!