Full-Stack Web Development with Vue.js and Node

(singke) #1
Building the Real Application Chapter 5

this.$swal('Oh oo!', `${message}`, 'error');
});
});
},
async fetchMovie() {
return axios({
method: 'get',
url: `http://localhost:8081/api/movies/${this.$route.params.id}`,
})
.then((response) => {
this.movie = response.data;
})
.catch(() => {
});
},
},
};
</script>

Let's also update the code to call the rate method when Rate this Movie is clicked. In


Movie.vue, update the following line of code:


...
<h6 class="card-title" v-if="current_user" @click="rate">Rate this
movie</h6>
...

Now, the last thing we need to do is add the rate endpoint in movies.js:


var Movie = require("../models/Movie");

module.exports.controller = (app) => {
// fetch all movies
app.get("/movies", function(req, res) {
Movie.find({}, 'name description release_year genre', function (error,
movies) {
if (error) { console.log(error); }
res.send({
movies: movies
})
})
})

// fetch a single movie
app.get("/api/movies/:id", function(req, res) {
Movie.findById(req.params.id, 'name description release_year
genre', function (error, movie) {
Free download pdf