Cannot solve problem 6 on backend course

Hi everyone, I’m trying to implement a solution for the problem 6 of backend course in FCC, but seems I cannot solve it no matter what.

This is my code:

app.get("/json", (req, res) => {
  const mySecret = process.env["MESSAGE_STYLE"];
  let text = "Hello json";
  if (mySecret === "uppercase") {
    text = text.toUpperCase();
  }
    res.json({ message: text });
});

Thanks in advance everyone :slight_smile:

Please link to your full project code and add a link to the specific challenge you are having trouble with.

Thank you.

This is the link of the challenge : https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file

This is my file:

let express = require("express");
let app = express();
require("dotenv").config();

// this send a file
app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
});
2;

// this append to the file a style file
app.use("/public", express.static(__dirname + "/public"));

// this send a json file
app.get("/json", (req, res) => {
  const mySecret = process.env["MESSAGE_STYLE"];
  let text = "Hello json";
  if (mySecret == "uppercase") {
    res.json({ message: text.toUpperCase() });
  } else {
    res.json({ message: text });
  }
});

module.exports = app;

Please link your project too - what platform are you using?

1 Like

Hi, sorry, I’m using Heroku https://node-test-freecodecamp.herokuapp.com/json

Do you have the full code on GitHub, so we can see all the files and code?

EDIT: I am not sure if you are seeing any errors in your app’s console, but I would check there first for any hint of the problem.

Also, since you are using Heroku, you need to take note of the last paragraph in the challenge instructions (see below).

If you are working locally, you will need the dotenv package. It loads environment variables from your .env file into process.env . Install it with npm install dotenv . Then, at the top of your myApp.js file, import and load the variables with require('dotenv').config() .

I do not see any reference to dotenv in the code you posted above, so I assume you did not take these steps to install dotenv and require it appropriately.

Other than what said above, I would add

Make sure to not include the /json endpoint when you submit

1 Like

GitHub - sebastianmariani/freeCodeCampBackEnd this is the repo in github :slight_smile:

I did, the problem is, when I follow the heroku link and navigate to /json the json that I get is all lower case

I cloned your project and added the necessary text in a .env file and it passes the tests no problem.

Can you show us exactly what it is in your .env file?

I haven’t used Heroku in a while but I would suggest you read the Heroku docs on config vars

2 Likes

This is the content of .env MESSAGE_STYLE=“uppercase”

right, that is a very good point

Ok it worked! I was trying to check the outcome of the message on heroku when visiting the link/json and I wasn’t able to see the capital HELLO JSON 'cause I didn’t have the config var setted, now I changed it and It worked and passes the test as well on FCC. Thanks a lot to everyone. :slight_smile:

Note for the future, if you have to write an env file again, no quotes

1 Like

You sometimes do quotes if there are special characters or a space in the value.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.