我有一个Angular应用程序需要能够从MongoDB集合中获取构造数据。我正在使用flConstruct中的$ resource服务从服务器请求数据。 “查询”调用返回所有数据,似乎工作正常。当我尝试使用'get'检索其中一个构造的数据时,不返回任何数据。
我已经打印到控制台以查看正在获取服务的内容,但我没有看到findOne函数可用的参数。以下是控制台的输出
Get - constructs.getConstructById
constructData.id undefined
req.params._id undefined
req.query._id undefined
No Error - Construct Data Retrieved
Get - constructs.getConstructs (Id) - No Error
Returned - construct null
Get - constructs.getConstructs (Id) - Sending Back question
Get - constructs.getConstructs (Id) - Sending Back neither error nor question
//名为的角色服务,flConstruct
angular.module('app').factory('flConstruct',function($resource){
var ConstructResource = $resource('/api/constructs/:id', {id: "@id"}, {
get: { method: 'GET', url: '/api/constructs/byId/:id', params: {id: "@id"}},
query: { method: 'GET' , url: '/api/constructs', isArray: true },
create: { method: 'POST'},
update: { method: 'PUT' },
delete: { method: 'DELETE', params: {id: '@id'}}
});
return ConstructResource;
});
//快速配置文件named,routes.js
var constructs = require('../controllers/constructs'),
module.exports = function(app){
app.get('/api/constructs', constructs.getConstructs);
app.get('/api/constructs/byId', constructs.getConstructById);
app.post('/api/constructs', constructs.createConstruct);
app.put('/api/constructs', constructs.updateConstruct);
app.delete('/api/constructs/:id', constructs.deleteConstruct);
});
}
// Express控制器名为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")
var constructData = req.body;
console.log("constructData.id " + constructData.constructId);;
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");
console.log("Get - constructs.getConstructs (Id) - Error");
res.send(null);
}else
{
console.log("No Error - Construct Data Retrieved");
console.log("Get - constructs.getConstructs (Id) - No Error");
//console.log("Returned - construct._id " + construct._id)
//console.log("Returned - construct.id " + construct.id)
console.log("Returned - construct " + construct)
console.log("Get - constructs.getConstructs (Id) - Sending Back question");
res.send(construct);
}
console.log("Get - constructs.getConstructs (Id) - Sending Back neither error nor question");
})
};
答案 0 :(得分:0)
我发现了自己的错误。它都在Angular代码中。在flCachedConstruct.js中对get进行的调用没有正确定义要传递的参数。我需要包含参数名称“id”以及参数值constructID。此函数调用中的“id”与我在flConstruct.js中调用的参数相匹配。
flConstruct.get({id: constructId }, function(data) {