Retrieve a collection of items based on the nested model's identification in MeanJS

I have developed a small app to store a list of episodes. There are tables for Series, Seasons, and Episodes. The Seasons table has a field "serie" which is an ObjectId reference to the Serie table. I have a dropdown select list with Serie items, and when I choose one item from the list, the getSeasons method is triggered. However, I am encountering a 400 Bad Request error.

Here are my files:

  1. series.server.routes.js

app.route('/serie/:serieId/seasons')
    .get(seasons.seasonsList);

  1. seasons.server.controller.js

exports.seasonsList = function(req, res, id) {
  Season.find({'serie': id}).exec(function(err, series) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.jsonp(series);
    }
  });
};

  1. episodes.client.controller.js

var Seasons = $resource('/serie/:serieId/seasons', {serieId: '@id'});
        Seasons.get({'serieId': $scope.serie}, function (data) {
          console.dir(data);
        });

Despite having set up the route, I am still receiving a 400 bad request error. Any idea why this might be happening?

I am aiming to achieve the same functionality as typing this command in mongo:

db.seasons.find({'serie': new ObjectId('SerieId')})

Answer №1

The issue was with the parameters. To resolve it, we need to modify the seasonsList method to retrieve the parameter from the request as shown below:

exports.seasonsList = function(req, res) {
  Season.find({'serie': req.params.serieId}).exec(function(err, series) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.jsonp(series);
    }
  });
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevented: Techniques for providing extra cushioning for a button, but with the condition that it contains an external icon

How can I apply padding to a button only if it contains an external icon? If the button has an external icon, I want to give it padding-right: 30px (example). However, if there is no external icon present, then the button should not have the 30px padding. ...

PHP redirect malfunctioning, yet still functioning?

After making some changes to the structure of my website, I seem to have broken the script somehow. When a user fills out a form correctly, they should be redirected to the appropriate page. However, the page just hangs. Strangely, the form works fine when ...

transmit an array variable using an ajax post call

I am currently facing an issue while attempting to process a form using an ajax request. The problem arises when dealing with an array variable within the form. Upon trying to serialize it for the ajax request, the result returned is as follows: email_id% ...

Error: Phonegap displaying incomplete or corrupted image

Currently, my Android application is being developed with Phonegap. Users have the ability to take photos that are then stored in a mysql database (medium-blob column) using a simple INSERT INTO query without altering the data. These images are then sent s ...

Utilize a solo input field to upload either a video or image, and showcase a preview of the uploaded content in a

I'm currently working on creating an input field that allows for the upload of both images and videos. Although I am able to successfully upload the files, I'm encountering an error when trying to display a preview. The code snippet below outline ...

Learn how to dynamically disable unchecked checkboxes and apply specific CSS classes to checked checkboxes in Angular 12 with TypeScript

Currently, I am working on a project where I have successfully stored checkboxes dynamically using a JSON array of objects. However, I am facing an issue that requires your expertise. My goal is to allow the selection of only 5 checkboxes. Once this limit ...

Exploring the World of Three.js Loaders

I downloaded the file package three.js-dev from threejs.org and I am trying to use it in my project like this: <script type="text/javascript" src="./build/three.js"></script> <script type="text/javascript" src="./src/loaders/JSONLoader.js" ...

What is the best way to securely store a client secret within a JavaScript application?

What is the best way to safeguard a client secret in a JavaScript application in order to ensure it is inaccessible to unauthorized users? Specifically, I am working with an AngularJS SPA. In my case, the client secret is a guid created during login and t ...

Issue with Material-UI Autocomplete: OnChange function causing interference with other functionalities

Handling the Autocomplete component has been a challenge for me, this is my current code snippet: <Autocomplete multiple id="checkboxes-tags-demo" options={ownerOptions2} disableCloseOnSelect filterSelectedOptio ...

What is the best way to retrieve all parameters in express.js, regardless of the request method used

Is there a way to access all parameters at once in express.js, instead of separately for GET and POST requests? I haven't been able to find anything on this yet, but it would be really useful for debugging purposes. While req.param(key) works well, ...

Informing ng2-bootstrap's Timepicker of the invalidation

I have integrated ng2-bootstrap's timepicker component into my project. To enhance user experience, I created a custom validation function that is triggered by the ngModelChange() event. However, the timepicker component also comes with its own built- ...

AngularJS Error: [$injector:modulerr] occurs when there is an issue with

I came across this Angular code snippet in the /public/javascripts directory of my Node project: var app = angular.module('userContent', ['angularUtils.directives.dirPagination']); app.controller('MainCtrl', [ '$scope&a ...

How to Uppercase the Keys of Each Object Element in Vue.js

I've got this particular item: { 'application': "BP 9ALT 8 123", 'address': "935 HAMPTON CRES", 'unit': null, 'status': "COMPLETED -ALL INSP SIGNED OFF" } I'm looking to tu ...

What is the best way to save information from an axios promise into my database on separate lines?

Having a technical issue and seeking assistance: Currently, I am encountering an issue with my axios request to the database. After successfully retrieving the data, I aim to display it in a select form. However, the response is coming back as one continu ...

Tips for transferring a data table (an object) from a JavaScript file to Node.js

On my HTML page, there is a table displaying student names and information, along with controls to manage the table. I created a save button to save this table as an object named SIT in my JavaScript code. I was able to manually save this table in MongoDB ...

Changing innerHTML in CoffeeScript

Are there other options instead of using the 'innerHTML' property in CoffeeScript? In JavaScript, you typically write something like this: document.getElementById('element').innerHTML = "blah_blah" Is there a different approach to ac ...

Issue with Angular Js: Trouble inserting array values post sorting

Hey everyone, I'm facing a challenge with AngularJS. I need to display the steps for selectionSort and here is my code: var myApp = angular.module('myApp',[]); var arraymain = []; myApp.controller('myController',function($scope) ...

Show component featuring checkboxes

Is it possible to show a component when a checkbox is clicked? I am creating a dashboard and need to choose which tools to display on my screen. I have set up checkboxes with v-model="select", value="name of component", and used v-for for list rendering. ...

Use jQuery to refresh the jQuery sparkline chart after fetching data asynchronously

Background Utilizing the jquery.sparkline library to generate Pie Charts. The data for these charts is stored in an array. Upon initial page load, a web-service call is made (using .ajax) to fetch the data. The callback function associated with this call ...

Obtaining documents through Mojolicious

Just a quick inquiry - I have successfully generated a .doc file using my mojolicious app with the help of the CPAN module MsOffice::Word::HTML::Writer. Now, the challenge I'm facing is figuring out how to make the browser initiate the download proces ...