angularjs - meanjs 路由到服务器 Controller

标签 angularjs node.js express meanjs

我正在尝试向我的 Controller “matches.server.controller”名称 listTeams 添加一个新方法。 我已经向 matches.server.route.js 文件添加了一条新路由,如下所示

'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);
};

这是我的服务器 Controller 中的方法:

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

在 matches.client.controller 中,我调用如下方法:

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

但是,当我调试时,我总是进入匹配列表方法,而不是进入 listTeams 方法 我做错了什么?

最佳答案

可能是因为你复制了路径名

'/matches/' 之后的所有参数均由

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

并感知'/:matchId'的参数

在您的情况下:找到 ID 为“listTeams”的团队

尝试将路径从 matches 重命名为 smth else,例如

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

关于angularjs - meanjs 路由到服务器 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591314/

相关文章:

mysql - Node.js:无法调用未定义的方法释放(MySql)

angularjs - 错误 : Unexpected value 'undefined' declared by the module 'DynamicTestModule'

javascript - 使用 Node js 运行不和谐机器人时出错

javascript - 如何自动将相对导入路径重写为绝对路径?

node.js - Express GET 路由不适用于参数

node.js - 如何有选择地调用中间件?

javascript - 优惠券验证

javascript - 尝试在一个数组中搜索另一个数组,其他数组具有内部优先级

javascript - 返回 ion-item 的 ajax 数据循环

node.js - 是否有任何查询可以通过检查 mongoDb 中嵌套嵌入文档的字段来查找文档