Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Can't update data based on id using express+mongo+node

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.

https://pastebin.com/kZLHAA5k

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)
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

Simple Quotes - Basic CRUD System

about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!