1

I have a MEAN.JS application. The code below is my AngularJS controller, which runs a query to retrieve all articles in my MongoDB database.

public/modules/articles/controllers/articles.client.controller.js

angular.module('articles').controller('ArticlesController', ['$scope', '$location', '$stateParams', 'Authentication', 'Articles',
    function($scope, $location, $stateParams, Authentication, Articles) {
        ...

        // Find a list of Articles
        this.articles = Articles.query();

        ...
    }
]);

The query above is defined in the file and code below:

app/controllers/articles.server.controller.js

exports.list = function(req, res) {
    Article.find().sort('created').populate('user', 'displayName').exec(function(err, articles) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(articles);
        }
    });
};

Articles.query() seems to be linked to exports.list. I want Articles.query() to use a different export, let's say exports.anotherList. How do I change this?

I have a feeling I have to define it somehow in the file app/routes/articles.server.routes.js, but I cannot figure out how to make it work.

Thanks. I'll accept the answer that helps me solve this.

Tom
  • 734
  • 2
  • 7
  • 27

1 Answers1

2

There are multiple ways to solve your problem. Like

  1. Configure your Article Service's query action for a new URL (for details: Angular Resource). Then define a route (for details: Express Routing) in your articles.server.routes.js for that URL like app.route('/articles/{new-path}').get(articles.anotherList) and define your functionality in articles.server.controller.js.

OR

  1. Try to rename exports.list to exports.anotherList in app/routes/articles.server.routes.js

Create a new function in articles.server.controller.js like

exports.anotherList = function(req, res) {
    //Write your instructions here
};

There can be other ways. Do what ever you like

  • Thank you for your reply. I went with your first method and I got it working now. In case anyone else has a similar question and doesn't know how to add multiple Service routes in AngularJS: http://stackoverflow.com/questions/17160771/angularjs-a-service-that-serves-multiple-resource-urls-data-sources – Tom Mar 30 '15 at 08:45