This community-built FAQ covers the “Code Challenge” exercise from the lesson “Code Challenges”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Web Development
Learn Express
FAQs on the exercise Code Challenge
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
I’ve been working on the express.js code challenges and have questions that hopefully somebody can help with.
For challenge number 6, here is my code https://gist.github.com/49bd6c5578762f4bc9447f2b9ecac9b4
My question is from line 32 where the code is:
currencies[currencyName] = newCountries;
I understand that this is assigning the new array from req.query to currencies[currencyName]. My question is doesn’t this erase the countries key from the currencies object? In my first solution, I wrote the code below:
currencies[currencyName][countries] = newCountries;
And it didn’t pass the test. Any thoughts on what happens to the original countries key in the currencies object? Doesn’t it get erased?
1 Like
I got stuck on this too.
They sending query with countries key and array. I think it’s not right way.
2 Likes
Please clarify the instructions or test on this exercise.
Currently, the instructions do not state that you must send req.query.name
. Yet, the test will only pass if you do so.
Thanks
3 Likes
Hi,
I resolved this challenge based on the previous example code:
monsters
const express = require("express");
const app = express();
const PORT = process.env.PORT || 4001;
const monsters = {
'1': {
name: 'cerberus',
age: '4'
},
'2': {
name: "Andrea",
age: "43"
}
};
app.use(express.static("Public"));
app.get("/monsters/:name", (req, res, next) => {
res.send("Béla");
});
app.put("/monsters/:id", (req, res, next) =>{
console.log(req.query);
const monsterUpdates = req.query;
let monster = monsters[req.params.id];
console.log(monster);
console.log(Boolean(monster));
if (monsters[req.params.id]) {
monsters[req.params.id] = monsterUpdates;
console.log(monsters);
res.send(monster);
} else {
res.status(404).send(`Cannot to update, Monster id ${req.params.id} not found.`);
}
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
});
I created a similar route for put request with the above example and passed the test.
You have to use an if condicional
to check the sent currency is exists or not. And only if exist can update, otherwise refuse the update.
For the Soups challenge, why do I have to use req.query.name, instead of just req.query?
const soups = [‘gazpacho’, ‘borscht’, ‘primordial’, ‘avgolemono’, ‘laksa’];
app.post(’/soups’, (req, res, next) =>{
const newSoup = req.query.name; //why not just req.query?
soups.push(newSoup);
res.status(201).send(newSoup);
});
The instructions were misleading. It doesn’t get erased because were replacing the whole object.
req.query = {countries: [country1, country2, etc..]}
So what we’re actually doing is replacing the whole object value of specified currency (/:name) since req.query returns an object not an array. Like most of you I tried accessing the array specifically with:
currencies[typeOfMoney].countries = update;
My answer:
app.put('/currencies/:name/countries', (req, res, next) => {
let typeOfMoney = req.params.name;
let update = req.query;
if (currencies[typeOfMoney]) {
currencies[typeOfMoney] = update;
res.send(currencies[typeOfMoney]);
} else {
res.status(404).send();
}
})
Its because req.query returns an object as a whole:
{name: newName}
You then have to access the name property of that object.
My answer:
app.post('/soups', (req, res, next) => {
let dishObject = req.query;
if (dishObject) {
soups.push(dishObject.name);
res.status(201).send(dishObject.name);
} else {
res.status(400).send();
}
})
The if statement checks to make sure that the object I received in req.query is complete and isn’t missing anything before moving on. Ruling out user error.
1 Like
Thank you - this code works well for me and I passed the challenge
1 Like
Hi there!
I’ve a just quick question. I might misunderstood something.
app.post('/soups', (req, res, next) => {
const name = req.query.name
soups.push(name);
res.status(201).send(name);
})
At the problem 7. this is the solution, But why I dont see something like ?name
in the app.post()
's url? How will the name
query will passed to the back-end?
Thank you!