I cannot change my JSON response to uppercase

Tell us what’s happening:
I cannot get the output response. I am using Replit.
My head is going in circles, does anyone have any idea? Thanks

Your code so far
https://replit.com/@lpninja/boilerplate-express

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

read these instruction carefully. if u using replit look at this instruction
Note : if you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.

then u can use it (idk how to use .env in replit cuz i using express) with process.env.NAME_VARIABLE, use if else for making that JSON uppercased

Thanks a lot. I am still not sure how to use this replit secrets tab.

it’s the tab on the left with a locket simble, add the required variable name as key and add the required variable value as value, then press add value

Got it thanks. Does this look right to you guys?

app.get("/json", (req, res) => {
const mySecret = process.env['MESSAGE_STYLE']
  if (process.env.mySecret !== "uppercase") {
    var response = "Hello World".toUpperCase();
    } 
      else  {response = "Hello World";
       
}
let message = response
//res.send(message)

  res.json({
  message: message
  });
});

you shouldn’t have an environment variable with name of mySecret, and you never use the constant declared

1 Like

The string is Hello json, not Hello World.

You might want to check your logic again. If it’s not uppercase you set it to uppercase, which is backward.

1 Like

thanks that is a silly mistake.

This replit method is still confusing me, does anyone have any idea on how to actually implement this? Many thanks

what’s your code now?

You are still not using the environment variable correctly. Use it in the if statement the way you are console logging it at the top.

Many thanks, it is currently:

var express = require('express');
var app = express();
console.log('hello world');
console.log(process.env.MESSAGE_STYLE)

//app.get("/", function(req, res) {

  //res.send("Hello Express");

  //res.send("Hello World");

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

app.get("/json", (req, res) => {

  if (process.env.MESSAGE_STYLE === "uppercase") {
    var response = "Hello Json";
    } else  {
      var response = "Hello Json".toUpperCase();    
}
       let message = response     

  res.json({
  message: message
  });
});

Oh thanks, I changed it to " process.env.MESSAGE_STYLE"

From the challenge description:

The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"} , depending on the MESSAGE_STYLE value.

spot the difference

1 Like

As said, you still have the if logic reversed.

  1. Don’t need this const mySecret = process.env['MESSAGE_STYLE'] if you have this if (process.env.MESSAGE_STYLE === "uppercase")

  2. Don’t redeclar response when you do not have to.

  3. Don’t mix var and let, use let. If you are using var because of how you are scoping and redeclaring the response variable that points to a lack of understanding. The rules for let are meant to help you avoid such issues. Don’t reintroduce them. Declare the variable using let outside the if/else code blocks and assign it the values inside the if/else code blocks.

  4. The line let message = response serves no purpose. If you wanted the response variable to be named message you should just do that.

2 Likes

I have simplified it and my JSON output does change. You mentioned that I had my IF logic reversed so I changed that. JSON does change if I change my SECRET part of Replit from {"message":"Hello json"} to {"message":"HELLO JSON"}
Here is the code. I feel that I am still being stupid and have something wrong.


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

app.get("/json", (req, res) => {

  if (process.env.MESSAGE_STYLE === "uppercase") {
    response = "Hello json".toUpperCase();
    } else  {
      response = "hello json";    
}

  res.json({
  message: response
  });
});

Where is response declared?

Ripping out all use of variable declarations is a bad idea.

In any case,

You have a typo in the capitalization here.

1 Like

Hi Jeremy,
Sorry I am a beginner in JavaScript. Where do you usually declare the response variable? At the top or inside the app.get area?
Here is my attempt.

app.get("/json", (req, res) => { 
...

It has something to do with how the handler function works.

The rule of thumb is to declare a variable to the smallest scope it needs. So here you can declare it inside of the get handler.

1 Like