我有一个Angular应用程序需要能够从MongoDB集合中获取构造数据。我有两个来自flConstruct的HTTP API调用(“query”和“get”)来从服务器请求数据。 “查询”调用返回所有数据,似乎工作正常。当我尝试仅检索其中一个构造的数据时,我收到错误。在服务器端,我有一个route.js,它应该将调用路由到我的constructs.js脚本中的正确函数。 “get”调用应该路由到construct.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);
})
};
答案 0 :(得分:1)
我发现了自己的错误。它都在Angular代码中。在flCachedConstruct.js中对get进行的调用没有正确定义要传递的参数。我需要包含参数名称“id”以及参数值constructID。此函数调用中的“id”与我在flConstruct.js中调用的参数相匹配。
flConstruct.get({id: constructId }, function(data) {