JSON response not passing

Tell us what’s happening:
I am converting the JSON response to uppercase case if the environment value is “uppercase”, but it still not working. Kindly help with this


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

  MESSAGE_STYLE === "uppercase"

    ? res.json({ message: "Hello json".toUpperCase() })

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

});

Your project link(s)

solution: boilerplate-express/myApp.js at gomix · Babajide777/boilerplate-express · GitHub

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

Challenge: Use the .env File

Link to the challenge:

Have you tried doing the .toUpperCase() logic before the res? I don’t know the exact logic but I don’t believe you can execute the function within that.

You need to check the MESSAGE_STYLE inside of this function to allow for changes in the secrets/env.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Good day Jeremy, but that is what I did. I checked if MESSAGE_STYLE equals “uppercase” and output { message: “HELLO JSON” } if true and { message: “Hello json”} if not. Is that what it is meant to be?

But where are you setting what this variable means? I don’t see that logic inside of the function.

Good day Castro, I changed my solution to this, but it doesn’t still work.

app.get("/json", (req, res) => {
  let message = "Hello json";

  if (MESSAGE_STYLE === "uppercase") {
    message = message.toUpperCase();
    res.json({ message });
  } else {
    res.json({ message });
  }
});

You still are not setting the value of MESSAGE_STYLE inside of this function.

This means that the value is only set once when your app first runs. That means that your code cannot respond to when the env changes.

This is it

const MESSAGE_STYLE = process.env.MESSAGE_STYLE;

I’ll just send the entire code

var express = require("express");
var app = express();
const path = require("path");
let public = path.join(__dirname, "/public");
require("dotenv").config();

const MESSAGE_STYLE = process.env.MESSAGE_STYLE;

app.use(express.static(public));

console.log("Hello World");

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "/views/index.html"));
});

app.get("/json", (req, res) => {
  let message = "Hello json";

  if (MESSAGE_STYLE === "uppercase") {
    message = message.toUpperCase();
    res.json({ message });
  } else {
    res.json({ message });
  }
});

module.exports = app;

Yep, and this is floating out in the global space, which means this code is only executed once. Which means the value of MESSAGE_STYLE never changes.

2 Likes

Solved. Thanks. Regards.

1 Like

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