I am currently working on an Angular application that needs to retrieve data from a MongoDB collection. To accomplish this, I am utilizing the $resource service within the flConstruct. The "query" function works well as it returns all data from the server without issues. However, when attempting to fetch data for a specific construct using the 'get' method, no data is being returned.
After examining the console output to see what the service is receiving, it appears that the parameter is not available for the findOne function. Here is the console log:
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
//Angular service named, 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;
});
//Express config file 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 controller named, 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");
}
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 " + 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");
})
};