I'm a totally newbie in node/express/mongo. I'm trying to learning but a single error is finishing my day. Don't know why. Googled it a lot still can't find any proper solution. Following is my code from pastebin where you can find my code.
I've the following error occurs all the time though I'm getting the id parameter from url using objectID for mongo. Can you please help me here...
Error:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (c:\Users\AD LORD\Desktop\learning-node\node_modules\bson\lib\bson\objectid.js:50:11)
at ObjectID (c:\Users\AD LORD\Desktop\learning-node\node_modules\bson\lib\bson\objectid.js:31:42)
at app.get (c:\Users\AD LORD\Desktop\learning-node\server.js:35:44)
at Layer.handle [as handle_request] (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\layer.js:95:5)
at next (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\layer.js:95:5)
at c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:281:22
at param (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:354:14)
at param (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:365:14)
As I didn't get any answer I somehow managed to resolve the issue. It seems my parameter was not set properly for which the error were coming so I've updated my route like the below one:
// update quote
app.post( '/update/edits', ( req, res ) => {
db.collection('quote').update (
{ _id: ObjectId(req.body.quoteID) },
{ $set: {
name: req.body.name,
quote: req.body.quote
}
}, function (err, result) {
if (err) {
req.flash('error', err);
} else {
req.flash('success', 'Quote updated successfully');
}
res.redirect('/');
});
});
By the way you can check the finished CRUD system in my github if it helps any other.
You can do it like so:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId(req.body.quoteID);
first please verify your req.body.quoteID is valid Id or not