Reputation: 31
I've looked around stack overflow and can't seem to find this error where the environment was similar to mine. I've tried to the best of my ability to isolate the source of the error but haven't had any luck. The error is as follows:
CastError: Cast to ObjectId failed for value "GetGelImages" at path "_id" at ObjectId.cast (/mean/node_modules/mongoose/lib/schema/objectid.js:117:13)
at ObjectId.castForQuery (/mean/node_modules/mongoose/lib/schema/objectid.js:166:17)
at Query.cast (/mean/node_modules/mongoose/lib/query.js:2340:32)
at Query.findOne (/mean/node_modules/mongoose/lib/query.js:1118:10)
at Query.exec (/mean/node_modules/mongoose/node_modules/mquery/lib/mquery.js:2213:16)
at Query.exec (/mean/node_modules/mongoose/lib/query.js:1797:19)
at exports.projectByID (/mean/app/controllers/projects.server.controller.js:426:142)
at paramCallback (/mean/node_modules/express/lib/router/index.js:378:7)
at param (/mean/node_modules/express/lib/router/index.js:358:5)
at Function.proto.process_params (/mean/node_modules/express/lib/router/index.js:384:3)
GET /projects/GetGelImages 500 17.173 ms - -
The code is following this basic structure: I have an html page that uses Angular to perform an $http.get Please note that I have significantly stripped these functions down in order to find the source of my error.
//HTML Button Implementation within the view
<button class="btn btn-primary" ng-click="getGelPhotos()">
View Gel Photos
<i class="glyphicon glyphicon-arrow-down"></i>
</button>
//AngularJS Function implementation within the controller
$scope.getGelPhotos = function(){
$http.get('/projects/GetGelImages').
success(function(req, res){
console.log('Successful get call.' + res.fileName);
}).
error(function(err){
alert('Error in retrieval of gel image.');
});
};
These portions seem to be working correctly. When I press the View Gel Photos button on the view, I receive the alert popup that indicates the error portion of the getGelPhotos function was called.
The express route:
'use strict';
/**
* Module dependencies.
*/
var users = require('../../app/controllers/users'),
projects = require('../../app/controllers/projects');
.
.
.
.
app.route('/projects/GetGelImages')
.get(users.requiresLogin, projects.retrieveGelImage);
I don't think the code is even making it to the express route however as it gave the exact same error when I completely removed the route.
Finally the server side NodeJS contained in the controller:
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
Project = mongoose.model('Project'),
Log = mongoose.model('Log'),
Plate = mongoose.model('Plate'),
Sample = mongoose.model('Sample'),
User = mongoose.model('User'),
_ = require('lodash'),
xlsx = require('xlsx'),
path = require('path'),
fs = require('fs'),
nodemailer=require('nodemailer'),
sys = require('sys'),
exec = require('child_process').exec,
multiparty = require('multiparty'),
util = require('util'),
http = require('http'),
uuid = require('node-uuid'),
express = require('express');
.
.
.
exports.retrieveGelImage = function(req, res) {
console.log('Made it to get gel image function');
};
I have not received this log message so far. I believe the problem lies in an intermediate space between Angular and the express route.
I'm new to the MEAN stack so if there's anything I can do to be more specific please let me know.
Thank you!
Upvotes: 0
Views: 648
Reputation: 31
Ok, so the answer was simple, although I'm not really sure why it worked. Here is the complete route list. I simply moved the Gel Retrieval Route form beneath the Gel Upload route to above it.
module.exports = function(app) {
// Project Routes
app.route('/projects')
.get(users.requiresLogin, projects.list)
.post(users.requiresLogin, users.hasOfficeStaffAuthorization, projects.create);
app.route('/projects/:projectId')
.get(users.requiresLogin, projects.read)
.put(users.requiresLogin, users.hasOfficeStaffAuthorization, projects.update)
.delete(users.requiresLogin, users.hasOfficeStaffAuthorization, projects.delete);
app.route('/projects/:projectId/GeneratePlateTemplate')
.post(users.requiresLogin, projects.generatePlateTemplate);
app.route('/projects/:projectId/GeneratePlates')
.post(users.requiresLogin, projects.generatePlates);
app.route('/projects/:projectId/UploadPlateLayout')
.post(users.requiresLogin, projects.uploadPlateLayout);
//Gel Retrieval Route------------------------------//ROUTE location matters!
app.route('/projects/:projectId/GetGelImages')
.get(users.requiresLogin, projects.retrieveGelImage);
//-------------------------------------------------
//Gel Upload Routes--------------------------------
app.route('/projects/:projectId/UploadGelImage')
.post(users.requiresLogin, projects.postImage);
app.route('/projects/:projectId/UploadGelImage/gridfs')
.post(users.requiresLogin, projects.storeImage);
//-------------------------------------------------
app.route('/projects/GenerateBarcodes')
.post(users.requiresLogin, projects.generateBarcodes);
app.route('/projectsByStatus/:projectStatus')
.get(users.requiresLogin, projects.listOfProjectsByStatus);
// Finish by binding the project middleware
app.param('projectId', projects.projectByID);
};
Upvotes: 1