简体   繁体   中英

meanjs route to server controller

i am trying to add a new method to my controller "matches.server.controller" names listTeams. I have added a new route to the matches.server.route.js file like this

'use strict';

/**
 * Module dependencies.
 */
var users = require('../../app/controllers/users.server.controller'),
matches = require('../../app/controllers/matches.server.controller');

module.exports = function(app) {
// Match Routes
app.route('/matches/listTeams')                 <-- new route added here !!
    .get(matches.listteams);


app.route('/matches')
    .get(matches.list)
    .post(users.requiresLogin, matches.create);


app.route('/matches/:matchId')
    .get(matches.read)
    .put(users.requiresLogin, matches.hasAuthorization, matches.update)
    .delete(users.requiresLogin, matches.hasAuthorization, matches.delete);

// Finish by binding the matches middleware
app.param('matchId', matches.matchByID);
};

this is the method in my server controller:

exports.listteams = function(req, res) {
    res.json(1);
};

In matches.client.controller i call the method like this:

 $scope.listteams = function(){
        $scope.teams = Matches.get('matches/listTeams').success(function(data){
            var d = data;
        }).error(function(data){
            var d = data;
        });

however when i debug i always come in the list method of matches and not in listTeams method What am i doing wrong?

Maybe it's because you are duplicating the path name

All parameters that goes after '/matches/' is handling by

app.route('/matches/:matchId')
    .get(matches.read)
    .put(users.requiresLogin, matches.hasAuthorization, matches.update)
    .delete(users.requiresLogin, matches.hasAuthorization, matches.delete);

and perceiving like argument of '/:matchId'

In your case: find me team with id "listTeams"

Try to rename your path from matches to smth else like

module.exports = function(app) {
// Match Routes
app.route('/smthelse_not_matches/listTeams')   <-- new route added here !!
    .get(matches.listteams);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM