Hey there! I have been parsuing the exercise tracker challenge and am stuck. Tried all there is but I keep failing 3 tests. Can someone kindly peruse my code and guide me please. Thanks
My code:
https://chipped-melon-seaplane.glitch.me
I think you may have forgotten a link to your code. [edit: Link now posted.]
@LuosRestil Guide me please. What link???
Hey @Bshaddie, I had a quick look at your code and it seems to me that you’re missing the part where you were supposed to persist your objects into your MongoDB database through Mongoose.
app.post("/api/exercise/new-user", (req, res) => {
const { username } = req.body;
const newUser = {
username,
_id: shortid.generate()
};
/*
'users' is an array. Pushing newUser object into an array
won't save it into your MongoDB database.
*/
users.push(newUser);
return res.json(newUser);
});
app.get("/api/exercise/users", (req, res) => {
return res.json(users);
});
app.post("/api/exercise/add", (req, res) => {
const { userId, description, duration, date } = req.body;
const dateObj =
date === "" ? new Date().toDateString() : new Date(date).toDateString;
const newExercise = {
username: getUsernameById(userId),
description,
duration: parseInt(duration),
_id: userId,
date: dateObj
};
/*
The same situation from above repeats itself here. That 'exercises', is an array.
*/
exercises.push(newExercise);
res.json(newExercise);
});
So you are pushing objects into arrays and use this mongoose method
UsernameAndId.findById(searchedUserId, (err, docs))
to search for users.
UsernameAndId
is supposed to be your mongoose model, right? But I couldn’t find neither the schema
nor the model
in your code. So I recommend you to start by first defining a Mongoose schema
for your object and then create a model
of it.
Don’t get me wrong, I might sound rude but I’m just trying to help you.
@krauss Not at all. You are not rude! Let me get down to it and see how it goes! Thanks
@krauss I have incorporated the schema and model but I can realise any changes in the test. Still failing the last 3.
Hey @Bshaddie, I’ve made something comments on your project. I hope that helps and guides you throughout the exercise. Trust me, it’s not that hard. You just really need to get hang of how MongoDB + Mongoose work together. Like I said on one the comments, focus on creating a newUser
a saving it into your database. That’s the path to accomplish all the other exercise’s requirements.
@krauss I have seen the comments you made thanx. I have tried to look out for the database_name but I seem not to locate or know how to find it. I have looked into my MongoDB account but I seem not to find it. Kindly guide me. Thanks
Don’t forget to check out the FCC guide on how to create a MongoDB account and login in to the MongoDB Atlas console. I have to admit that when I started MongoDB and Mongoose module from APIs and Microservices Certification, and I too had skipped the introduction part
without knowing that it was essential for the success of the all the future projects. So, I went and carefully did it step-by-step. The outcomes of understanding MongoDB and Mongoose were great, it made the 6 last projects so much easier to me. So much so, that I finished my Full Stack Certification two days ago
. So, put all your focus on those guys for now mate, MongoDB and Mongoose. I loved learning about them and I hope you too. P.S: Relational databases never was my thing haha
.
@krauss
I have tried to do the necessary amendments according to the guideline but I still can’t break even with the challenge. Kindly look at my code again and advise accordingly:
https://chipped-melon-seaplane.glitch.me
Thanks
As you might’ve seen, I’ve done some refactoring in your code just to make it easy for you to grasp the beautiful relationship between MongoDB & Mongoose hahaha.
From now on, Mongoose Docs will be your master guide. Of course, if you still need help, come back here and give us a shout. Just try to be as straight forward as possible with your questions/doubts because we can’t, and shouldn’t, keep coding for you all.
If coding is your thing, have fun and don’t give up. You’ll get there…
Hey B;
I agree with what krauss said, but I’m going to attempt to drill down into the details a bit to test my own comprehension of it too - the key parts of using the DB is the following:
mongoose.connect()… to connect to your database, then define your Schema - I’ve called trackerSchema
const TrackerModel = mongoose.model("TrackerModel", trackerSchema);
console.log(mongoose.connection.readyState);
var tracker=new TrackerModel({username:username};
// remember to put inside an async function:
await tracker.save(err => {
if(err){ return err;
}else{ return res.send(tracker)
The key bit of code above is that my collection(database) is called “TrackerModel” , trackerSchema defines how documents are stored, and my instance of TrackerModel is called tracker- this instance gets loaded up with data as per the Schema and when you call tracker.save that saves this tracker to the TrackerModels Collection visible in MongDB Atlas…play around on there to see how to view update and delete the DB manually.
Tricky part here:
Now if you want to look up data on your collection,(or update/delete) you use TrackerModel.findById or TrackerModel.delete…ect So use tracker to save, but TrackerModel to access/view/update/delete .
Hope that helps?
Paul