CastError:对于值#34,GetGelImages"在路径" _id"使用MEAN Stack

时间:2015-03-26 20:44:16

标签: angularjs node.js express mean

我已经查看了堆栈溢出,并且在环境与我的环境相似的情况下似乎无法找到此错误。我已尽最大努力隔离错误来源,但没有运气。错误如下:

  

CastError:施放到ObjectId的值失败" GetGelImages"在路径" _id"       在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 - -

代码遵循这个基本结构:我有一个使用Angular执行$ http.get 的html页面。请注意我已经明显剥离了这些函数,以便找到我的错误来源。< / em>的

//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.');
        });
    };

这些部分似乎正常工作。当我按下视图上的“查看凝胶照片”按钮时,我会收到警告弹出窗口,指示调用了getGelPhotos函数的错误部分。

快递路线:

'use strict';

/**
 * Module dependencies.
 */
var users = require('../../app/controllers/users'),
    projects = require('../../app/controllers/projects');
.
.
.
.

app.route('/projects/GetGelImages')
        .get(users.requiresLogin, projects.retrieveGelImage);

我不认为代码甚至可以进入快速路线,但是当我完全删除路线时它会给出完全相同的错误。

最后控制器中包含的服务器端NodeJS:

'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');
};

到目前为止,我还没有收到此日志消息。我认为问题在于Angular和快速路线之间的中间空间。

我是MEAN堆栈的新手,所以如果我能做些什么来更具体,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,答案很简单,虽然我不确定为什么会有效。这是完整的路线列表。我只是将凝胶上传路线下方的凝胶检索路线表格移到了它上面。

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);
};