Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jonas Mongo API #520

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Jonas Mongo API #520

wants to merge 9 commits into from

Conversation

Jonash189
Copy link

@Jonash189 Jonash189 commented Dec 16, 2024

- Set up Express server with MongoDB using Mongoose
- Added dotenv for managing environment variables
- Seeded Netflix titles from JSON file into MongoDB
- Implemented endpoints for fetching all titles, filtering by query parameters (country, release year, rating), and fetching a specific title by ID
- Resolved issues with JSON imports and pretty-printing responses
- Addressed challenges with properly formatting and querying data
- Refactored and debugged code to ensure correct functionality
@Jonash189
Copy link
Author

I’ve encountered an issue where my routes return empty arrays when deployed to Render. Locally, everything works as expected, but the data doesn’t show up in MongoDB Atlas.

I’m not sure what the issue is — it could be that the seeding function isn’t running correctly, the connection string is misconfigured, or there’s a problem with how the data is being handled. I’ve checked my JSON file and the seeding logic, and both seem fine, but I can’t figure out why the netflix_titles collection isn’t appearing in Atlas.

@HIPPIEKICK @JennieDalgren

@HIPPIEKICK
Copy link
Contributor

HIPPIEKICK commented Dec 16, 2024

I’ve encountered an issue where my routes return empty arrays when deployed to Render. Locally, everything works as expected, but the data doesn’t show up in MongoDB Atlas.

I’m not sure what the issue is — it could be that the seeding function isn’t running correctly, the connection string is misconfigured, or there’s a problem with how the data is being handled. I’ve checked my JSON file and the seeding logic, and both seem fine, but I can’t figure out why the netflix_titles collection isn’t appearing in Atlas.

@HIPPIEKICK @JennieDalgren

Have you followed this guide? Did you put your reset database variable to true in your env file together with your mongo connection string? If yes, can you console.log what those values are in your server.js file?

With this "Locally, everything works as expected, but the data doesn’t show up in MongoDB Atlas." - do you mean that you can access your Atlas database locally? If it's just that you cannot access it on Render, you probably haven't added the correct mongo string on Render (as an env variable).

@Jonash189
Copy link
Author

You were right, @HIPPIEKICK – it was my environment variable on Atlas that was incorrect. I’ve fixed it, and now Atlas can successfully connect to my database.

After 4-5 hours of troubleshooting and making changes to the code to get it working, I noticed that my commits never went through. When I realized the pushes weren’t going through, I tried something called push --force and probably a few other commands, and after that, nothing worked at all. But at least the database and the different routes are now working, so there shouldn’t be any issues with the task’s goal. However, if anything else needs to be changed, I’ll have to redo the project since, no changes are going through ;(

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at your deployed link to see if you can get it to work (I can't). Also, I'd probably implement pagination since the dataset is huge (but see that as a stretch goal).

I need to see a working deployment before I can approve, but I saw your comment on having problems pushing. Could you try to clone your repo anew? If you can't get it to work, please let us know of any errors you get and we'll try to help you

Comment on lines +10 to +16
try {
const rawData = fs.readFileSync("./data/netflix-titles.json");
netflixData = JSON.parse(rawData);
console.log(`✅ Loaded ${netflixData.length} titles from JSON file.`);
} catch (error) {
console.error("❌ Error reading or parsing JSON file:", error.message);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for? 👀

Comment on lines +139 to 152
app.get("/netflix_titles/:id", async (req, res) => {
const { id } = req.params;

try {
const title = await NetflixTitle.findOne({ show_id: Number(id) });
if (title) {
res.json(title);
} else {
res.status(404).json({ error: "Title not found" });
}
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this endpoint to work on your deployed version, can you?

Comment on lines +117 to +123
const query = {};
if (country) {
query.country = { $regex: country, $options: "i" };
}
if (release_year) {
query.release_year = Number(release_year);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice query-building 💯

@Jonash189
Copy link
Author

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the descriptive screenshots, I realised that I searched for the Mongo ID and not the show_id 😅 All is good, sorry for the hassle!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants