Reputation: 688
Im having some issues with deleting an entry from mongo DB. Im using node-mongodb-native
The code with a issue is
ArticleProvider.prototype.delete = function(id, callback) {
this.getCollection(function(error, article_collection) {
if( error ) callback(error)
else {
article_collection.findAndRemove({_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error)
else callback(null, result)
});
}
});
};
its a strange issue because i have a function to return a single article that is
ArticleProvider.prototype.findById = function(id, callback) {
this.getCollection(function(error, article_collection) {
if( error ) callback(error)
else {
article_collection.findOne({_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error)
else callback(null, result)
});
}
});
};
and that works like a charm
this is my error
500 TypeError: Cannot read property 'length' of undefined
at Function.createFromHexString (/Users/username/express_blog/node_modules/mongodb/lib/mongodb/bson/objectid.js:226:22)
it seems to be an issue with the type of id (or it seems).
Upvotes: 0
Views: 1380
Reputation: 6602
The id
argument you are passing must be undefined.
Here is the source to the current version of that function , or the newest one I could find on github.
Note that the framework code here does not handle (typeof hexString === 'undefined')
ObjectID.createFromHexString = function createFromHexString (hexString) {
// Throw an error if it's not a valid setup
if(hexString != null && hexString.length != 24) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format");
var len = hexString.length;
if(len > 12*2) {
throw new Error('Id cannot be longer than 12 bytes');
}
var result = ''
, string
, number;
for (var index = 0; index < len; index += 2) {
string = hexString.substr(index, 2);
number = parseInt(string, 16);
result += BinaryParser.fromByte(number);
}
return new ObjectID(result);
};
Upvotes: 1