Mogo references and endpoints
I am trying to make an endpoint display a name when i am using the ObjectID reference to filter the data. I have the Schema.
let movieSchema = mongoose.Schema({
Title:{type: String, required:true},
description: {type: String, required:true},
genre: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Genre'}],
director: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Director'}],
Actors: [String],
imageUrl: String,
featured: Boolean
});
let genreSchema = mongoose.Schema({
name: {type: String, required: true},
description: String
});
I currently have the endpoint to display all movies with the genre ID like so.
app.get('/movies/:genre', passport.authenticate('jwt', {session:false}), (req, res) => {
const genre = req.params.objectID;
Movies.find({ 'genre.type':genre })
.then((movies) => {
res.status(201).json(movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
What i am hoping to achieve and do not know where to start with this, it to have the endpoint not use the actual ID but the name instead. So i can still use the reference in the database but instead i could use the endpoint "/movies/thrillers" instead of "/movies/601511816..." ect.
Is this possible?
from Recent Questions - Stack Overflow https://ift.tt/34oKvt7
https://ift.tt/eA8V8J
Comments
Post a Comment