Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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

over 4 years ago · Santiago Trujillo Relatório

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda