【问题标题】:Angular HTTP API get request not being routed to the correct Express server functionAngular HTTP API 获取请求未路由到正确的 Express 服务器函数
【发布时间】:2016-11-20 22:01:20
【问题描述】:

我有一个 Angular 应用程序,它需要能够从 MongoDB 集合中获取构造数据。我有两个从 flConstruct 发出的 HTTP API 调用(“query”和“get”)来从服务器请求数据。 “查询”调用返回所有数据并且似乎工作正常。当我尝试仅检索其中一个构造的数据时,出现错误。在服务器端,我有一个 route.js,它应该将调用路由到我的constructs.js 脚本中的正确函数。 “get”调用应该路由到constructs.getConstructById,但似乎是路由到constructs.getConstructs。谁能看到我可能做错了什么?

我收到的错误消息是“flCachedConstructs.queryID(...).$promise is undefined”,但我认为这是一个误导性消息,因为检索到的(集合)与预期的(单个文档)不同。

//flCachedConstructs

angular.module('app').factory('flCachedConstructs', function(flConstruct,$http, $q) {
var constructList = null;  // temporarily set to null for testing purposes
var construct;

return {
    queryID: function(constructId) {
        console.log("flCachedConstruct - queryID function by ID - start");
        console.log("flCachedConstruct - queryID function by ID - parameter = constructId " + constructId);
        var deferred = $q.defer();
        flConstruct.get(constructId, function(data) {
            if (data) {
                deferred.resolve(data);
            }
            else {
                deferred.reject("Error getting Construct");
            }
        });
        return deferred.promise;
    },
    queryAll: function() {
        console.log("flCachedConstruct - queryAll function, no parameters - start");
        if(!constructList || constructList == null) {
            console.log("flCachedConstruct - querAlly function - if constructList is null or does not exits");
            constructList = flConstruct.query();
        }else
        {
            console.log("flCachedConstruct - queryAll function - if constructList is not null and does exits");
            console.log("constructList = " + constructList );
        }

        return constructList;
    },
    empty: function() {
        console.log("flCachedConstruct - empty function - start");
        constructList = null;
        console.log("flCachedConstruct - empty function - after setting constructList to null");
        return constructList;
    }

}
})


//flConstruct.js

angular.module('app').factory('flConstruct',function($resource){
var ConstructResource = $resource('/api/constructs/:id', {_id: "@id"}, {
    query: { method: 'GET', isArray: true },
    get: { method: 'GET', params: {id: '@id'} , isArray: false },
    create: { method: 'POST'},
    update: { method: 'PUT' },
    delete: { method: 'DELETE', params: {id: '@id'}}
});

return ConstructResource;
});


//routes.js

var constructs = require('../controllers/constructs'),

module.exports = function(app){


app.get('/api/constructs', constructs.getConstructs);
app.get('/api/constructs/:id', constructs.getConstructById);

app.post('/api/constructs', constructs.createConstruct);
app.put('/api/constructs', constructs.updateConstruct);
app.delete('/api/constructs/:id', constructs.deleteConstruct);


app.all('/api/*', function(req,res){
    res.sendStatus(404);
});

app.get('*', function ( req, res) {
    res.render('index', {
        bootstrappedUser: req.user
    });
});
}



// construct.js

var Construct = require('mongoose').model('Construct');

exports.getConstructs = function(req,res){
var constructData = req.body;
console.log("Get - constructs.getConstructs");
console.log("constructData.id " + constructData.id);
console.log("constructData " + constructData);
console.log("req.params.id " + req.params.id);
console.log("req.query.id " + req.query.id);
Construct.find({}).exec(function(err,collection){
    if (err){
        console.log("Error - No Construct Retrieved");
    }else
    {
        console.log("No Error - Construct Data Retrieved");
    }
    //console.log(collection);
    res.send(collection);
})
};

exports.getConstructById = function(req,res) {
console.log("Get - constructs.getConstructById")
console.log("req.params._id " + req.params._id)
console.log("req.query._id " + req.query._id)
Construct.findOne({_id:req.params.id}).exec(function(err,construct){
    if (err){
        console.log("Error - No Construct Retrieved");
    }else
    {
        console.log("No Error - Construct Data Retrieved");
    }
    res.send(construct);
})
};

【问题讨论】:

  • 我已将问题隔离到 Angular 代码。如果我在 flConstruct.js 函数的 get 中对 id 进行硬编码,一切正常。问题是参数没有被传递到 flConstruct.js 的 get 中。我无法理解为什么?

标签: angularjs node.js api http express


【解决方案1】:

我发现了我的错误。这一切都在 Angular 代码中。在 flCachedConstruct.js 中对 get 的调用未正确定义要传递的参数。我需要包含参数名称“id”以及参数值constructID。此函数调用中的“id”与我在 flConstruct.js 中调用的参数匹配。

flConstruct.get({id: constructId }, function(data) {

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 2014-05-06
    • 2020-11-24
    • 2015-12-16
    • 2020-03-12
    • 2016-09-13
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多