In the MEAN.io framework, what is the relationship between public controllers and server controllers?

Just getting started with MEAN.io and they include a sample "articles" model that resembles a typical blog post with a title and body.

Included in the example is an index.html file that showcases a list of articles upon navigation. Within this file, there's a call to a find method defined in the public controller like so:

$scope.find = function() {      
  Articles.query(function(articles) {
     $scope.articles = articles;
  });      
};

I came across a server controller that contains the following method definition:

/**
 * List of Articles
 */
exports.all = function(req, res) {
  Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
    if (err) {
      return res.json(500, {
        error: 'Cannot list the articles'
      });
    }
    res.json(articles);
  });
};

By adding a constraint to the find method in the server controller, I found that I could specify where filters for the query, resulting in changes reflected in the view.

Is there an inherent connection between these two controllers that the framework manages behind the scenes? I've been unable to locate any information detailing their relationship.

Answer №1

In my opinion, I don't think so. If there were a filtering connection, the code would probably look something like this:

/**
 * A collection of blog posts
 *  use GET /api/v1/posts?tag=technology to filter
 */
exports.allPosts = function(req, res) {
  Post
        .find(req.query) //this part filters out the posts
        .sort('-createdAt')
        .populate('author', 'name username')
        .exec(function(err, posts) {
    if (err) {
      return res.json(500, {
        error: 'Could not retrieve posts'
      });
    }
    res.json(posts);
  });
};

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

Ways to decrease the size of this item while maintaining its child components?

Here is an object that I am working with: { "name": "A", "children": [ { "name": "B", "open": false, "registry": true, "children": [ { ...

Maintaining a reference to an element while binding event handlers in a prototype

Is there a way to utilize a bound event handler in prototype while maintaining the "this" context? It seems that doing so disrupts the default binding provided by prototype for event handlers. According to the documentation: The handler's context ...

accessing the offsetTop property of React elements

When working in React, I've noticed that the offsetTop value of an element differs between componentDidMount and componentDidUpdate. This is surprising to me because I believed componentDidMount occurs after render, meaning the DOM elements should be ...

Access the blob file saved in the indexedDB on an iOS device or iPad

Greetings, I am currently fetching a file using axios in the following manner: return axios({ method: "get", url: URL, responseType: "blob", }) .then((response) => { return { ...val, ...

Challenges faced with react-bootstrap-autosuggest

After spending the entire day attempting to integrate the package from here into my create-react-app project upon ejection, I encountered the following error: Failed to compile. Error in ./~/react-bootstrap-autosuggest/lib/Autosuggest.js Module not found ...

How can I access a global variable in a node/express application on both the client and server side?

Imagine having a variable, x, which is initialized in the request as req.x in an express middleware. To make this variable accessible to the client via the template, we use <%- window.x = req.x %>. Now, if we want to use this variable globally, it c ...

Invoking PHP function through Ajax

I'm having trouble with a PHP function not running when using AJAX. Although the AJAX post request is working correctly, here's the code snippet: function thumbs(i) { $('.thumbs-up' + String(i)).click(function(){ $(this).a ...

What is the best way to incorporate a vanilla javascript function into a vue.js application?

Consider a vanilla JavaScript function like this: if (window.devicePixelRatio >= 2) { document.querySelectorAll('img.retina').forEach(function (e) { let parts = e.src.split('.'); let ext = parts.pop(); i ...

Refine your MongoDB collections with multiple filtering options

My dataset looks like this: [ {"userId": "0000", "algorithm": "algo1", "status": "Running", "waitingTime": 0}, {"userId": "0001", "algorithm": &qu ...

What is the best way to submit a Redux Form only if there have been changes made to any of the fields

I'm currently using Redux Form version 7.1.2, and I have a form that submits data when the user clicks the submit button. In the function assigned to handle the submission, I perform an action. However, I only want this action to be executed if there ...

The jQuery animation concludes before its anticipated completion

I'm currently facing a small issue with a jQuery animation. The HTML code I have is as follows: <div id="menu"> <a id="menu-about" href="/">About...</a><br /> <a id="menu-ask" href="/">Ask me a question</a> ...

Is There a Sparse Array Element in JavaScript?

Is there a more efficient method to check for the presence of an array element within multiple layers of a structure? For example: if (typeof arr[a][b][c] === 'undefined') { ...do something... } If [a] or [b] are missing, we cannot accurately t ...

Utilizing a function as a value in React state (setState) compared to defining state with constructor in a class and utilizing a state object

Can someone help me understand the concept of state in React better? I'm confused about the differences between these two implementations: This: class Todo extends ... { constructor (){ super() this.state = { ... } } } And This: class Todo extend ...

Guide on transforming an array object for compatibility with MUI's Autocomplete field

I've encountered a challenge while attempting to transform my incoming object into a format suitable for MUI's Autocomplete component. Here is the current code snippet I am working with: const [contactList, setContactList] = useState([]); useEf ...

Tips for pressing the enter key to submit when faced with two buttons

I am developing a form with two input fields and their respective submit buttons. I want users to be able to enter text into either field, hit the Enter key, and have it trigger the same action as clicking the submit button. Currently, pressing Enter after ...

Is there a way to assign a key value to a variable in my code?

An HTTP request contains data with the URL parameter '/r/rr-autocomplete/?q=string': {"results": [{"id": "371", "text": "strstr", "selected_text": "strstr"}], "pagination& ...

Sending data using the AJAX method with integers

Utilizing AJAX to send a high score to a SQLite database's highScores table, the total of the high score must be calculated through a query and then retrieved back from the database. How can I ensure that the score is sent as an integer through AJAX t ...

What is the best way to verify the existence of a remote image in AngularJS?

Here is an example of the code snippet : <div ng-repeat="u in users"> <!--An active URL will always be received --> <div ng-if="u.imageUrl"> <!--Check if the provided URL is active--> <!--Content only visible if image url is li ...

Input values in Angular are not being updated according to the corresponding model values

My Angular application features two routes that share the same controller and partials. Despite the shared code, the controller and partials behave slightly differently depending on the route being used. Here are the routes: $routeProvider.when('/joi ...

Limiting the amount of blogs shown on a single page can be achieved with Yii 1 and PHP

I have successfully implemented a feature to display blogs on one page. However, I now want to modify it so that only 5 blogs are shown per page and the user can click on a "next page" button to view the next set of 5 blogs. Here is the code snippet from ...