Reputation: 448
I am trying to remove a record from MongoDB using nodeJS. But the record is not getting deleted.
Please find the below code:
exports.remove = function(studentId, cb) {
var collection = db.get().collection('students');
collection.remove({_id: studentId}, function(err) {
if (err) {
throw err;
}
else {
cb(err);
console.log("Record deleted.");
}
});
}
I have tried the studentId with ObjectID() as below:
exports.remove = function(studentId, cb) {
var collection = db.get().collection('students');
collection.remove({_id: new mongodb.ObjectID(studentId)}, function(err) {
if (err) {
throw err;
}
else {
cb(err);
console.log("Record deleted.");
}
});
}
But getting an error as : "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" Please help on this issue!!!!!
Upvotes: 0
Views: 998
Reputation: 448
I got the solution for this, why I was getting the error - "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" when using new mongodb.ObjectID(studentId). I want this to be shared with all of us.
My angularJS controller is as follows:
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.";
});
}
});
In the above controller we can see the URL as
var urlDelete = "/students/remove:"+$scope.studentRadio;
which inturn calls my node controller :
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);
}
});
});
The Angular code is setting the endpoint like this:
"/students/remove:"+$scope.studentRadio
I want the : to be there, so the URL will look something like this:
/students/remove:576c1d4781aaa4f16a68af24
The Express route is as below:
router.delete("/remove:studId", ...)
: is a special character in Express routes (it declares a named parameter called studId). This means that the route will take everything after /remove to be the value of studId, including the colon that's in the URL. So req.params.studId is :576c1d4781aaa4f16a68af24, which has a length of 25 characters.
If we want to use this sort of URL scheme, we need to make the colon to be part of the match by escaping it (so it loses its special meaning):
router.delete("/remove\::studId", ...)
Upvotes: 0
Reputation: 6672
perhaps creating the id from this method will help :
mongodb.ObjectID.createFromHexString(studentId);
https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html
Upvotes: 0
Reputation: 526
get()
method on db:
var collection = db.collection('students');
new
keyword, it's just fine to only wrap:
collection.remove({_id: mongodb.ObjectID(studentId)}, function(err) {
Upvotes: 0
Reputation: 1267
When you call your remove function, make sure you are passing an actual string as the first argument, not a numeric.
Or, in your remove function, cast studentId to a string like so:
collection.remove({_id: studentId.toString()}, function(err) {...
Upvotes: 0