MongoDB _id长度为25

我正尝试使用nodeJS从MongoDB中删除一个文档。

我的代码stream程如下:1.用户可以删除特定的学生。 因此,我把所有的学生文件从数据库中提取出来,存储在学生对象中。 3.用户可以使用提供的文本框过滤search,并可以select一个特定的学生,并可以删除。

请find下面的UI:

UI

HTML页面如下:

<h2 align="center">Delete Student</h2> <div ng-controller="deleteStudentController"> <form ng-submit="deleteStudent()"> Student Name:<input type="text" letters-only ng-model="searchName"/><br> <div ng-repeat="student in students | filter:searchName" ng-show="searchName.length"> <input type="radio" ng-model="$parent.studentRadio" name="studentRadio" value="{{student._id}}"/>{{student | formatter}} </div> <input type="submit" value="Delete Student"/> {{output}} </form> </div> 

与UI相关的angularJS控制器如下所示:

 mainApp.controller("deleteStudentController", function($scope,$http) { var resData = {}; $scope.student = {}; var urlGet = "/students/all"; $http.get(urlGet) .then(function(response) { $scope.students = angular.fromJson(response.data); }); $scope.deleteStudent = function(){ var urlDelete = "/students/remove:"+$scope.studentRadio; $http.delete(urlDelete) .success(function(response, status, headers, config){ $scope.output = "Student successfully deleted."; }) .error(function(response, status, headers, config){ $scope.output = "Error in processing. Please try again."; }); } }); 

哪个inturn调用节点控制器:

 router.delete("/remove:studId", function(req,res){ Students.remove(req.params.studId, function(err) { if (err) { throw err; } else { var respOut = "Student deleted"; res.send(respOut); } }); }); 

在控制器内部,有一个学生模型的对象,在这个对象中进行数据库通信。 我已经使用mongodb.ObjectID.createFromHexString()方法来创buildobjectID从_id值,我从数据库

学生模型代码删除文件:

 exports.remove = function(studentId, cb) { var collection = db.get().collection('students'); console.log("_id"+studentId); console.log("Length---->"+studentId.length); collection.remove({_id: mongodb.ObjectID.createFromHexString(studentId)}, function(err) { if (err) { throw err; } else { cb(err); console.log("Record deleted."); } }); } 

但是,它将错误提供为“错误:传入的参数必须是12个字节的单个string或24个hex字符的string”。 所以我试图logging从DB学生文档中获得的_id值及其长度。 令人惊讶的是,我得到的长度为'25'。

请查看下面的控制台,我得到:

_id:576aba09090560f80bd2caaa长度—-> 25

有关这个问题的任何想法? 请帮忙…..

你的Angular代码是这样设置端点的:

 "/students/remove:"+$scope.studentRadio 

我假设你想:在那里,所以这个URL看起来像这样:

 /students/remove:576c1d4781aaa4f16a68af24 

您的快速路线如下所示:

 router.delete("/remove:studId", ...) 

:是Express路由中的一个特殊字符(它声明一个名为 studId的命名参数 )。 这意味着你的路由将把/remove后的所有内容都作为studId的值, 包括 URL中的冒号。 所以req.params.studId:576c1d4781aaa4f16a68af24 ,长度为25个字符。

如果你想使用这种URLscheme,你需要通过转义冒号来使冒号成为匹配的一部分(所以失去了它的特殊含义):

 router.delete("/remove\\::studId", ...)