I don't understand why my api is returning unknown/undefined when I call it using app.get

Julian

I'm learning backend and here is an exercise regarding api/express/node.js

The problem is app.get("/api/persons/:person"... I'm console logging personName, which is fine, and persons, which is also fine. But when I do persons[personName], it returns undefined.

The result should be returning an individual json object of the person's information.

const express = require("express");
const app = express();
const cors = require("cors");
const PORT = process.env.PORT || 3001;

app.use(cors());

let persons = {
  "Arto Hellas": {
    id: 1,
    name: "Arto Hellas",
    number: "040-123456",
  },
  "Ada Lovelace": {
    id: 2,
    name: "Ada Lovelace",
    number: "39-44-5323523",
  },
  "Dan Abramov": {
    id: 3,
    name: "Dan Abramov",
    number: "12-43-234345",
  },
  "Mary Poppendieck": {
    id: 4,
    name: "Mary Poppendieck",
    number: "39-23-6423122",
  },
  "unknown": {
    id: "unknown",
    name: "unknown",
    number: "unknown"
  }
};

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

app.get("/api/persons", (req, res) => {
  res.json(persons);
});

app.get("/api/persons/:person", (req, res) => { // <-- here is the problem
  const personName = req.params.person.toLowerCase();

  console.log(personName);
  console.log(persons);
  console.log(persons[personName]);

  if (persons[personName]) {
    res.json(persons[personName]);
  } else {
    res.json(persons["unknown"]);
  }
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
Mureinik

personName is converted to lower case, while the keys in persons are not in lower case. Either change the keys in persons to be lower case, or remove the toLowerCase() call when extracting the personName and call your API with the proper case for the names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why don't I get any syntax errors when I execute my Python script with Perl?

I don't understand why I get this warning about conversion between signed and unsigned, is my compiler wrong?

I don't understand why my group by is failing

I don't understand why my code can't adopted css when i try

Why don't I get an segmentation fault when I overflow a vector with my custom allocator?

My code returns a StackOverFlowException when I enter a name... I don't understand why

Returning None in flask, but I don't understand why

I don't understand why my for loop return "undefined"

I don't understand why using " " in my code works, but the same code with ' ' does not

Why my app throw error when i call the coronavirus API?

Why do I get a conversion failed error when I use my attribute on my WHERE clause but it works when I don't?

Why don't I get the same result as with tensorflow's method when I write my own expression?

My Selenium Webdriver script is returning an error that I don't understand

Why don't I get an infinite loop when I call exit from an atexit handler?

Don't understand why do I get a permission denied error

i dont understand why my "if" command don't work as it should

I don't understand using get-content on a csv

My code outputs an error, I don't understand why

Why don't I get an input when I call the function?

I don't understand why this code works this way in my code

I don't understand why my verification middleware behaves this way

I don't understand why my answer by hand is different

ESLint complains my code format I don't understand why

I don't understand why my for loop code works

Using htons() in my code puts all zeros in the buffer and I don't understand why

My variables aren't dividing and I don't understand why

I don't understand why my sort on a string breaks everything

I don't understand why my code is looping when not supposed to

I don't understand why my list's value is reset