Auto-fill inputs in the edit form with old data

Asked

Viewed 118 times

0

Well, I’m using Node.js and Mongoose (mongoDB) as a framework for the database. On the front end I’m using handlebars. The problem is that the inputs are empty even passing as parameter the object. Below the code in Avascript and the form:

// Código em javaScript que renderiza o formulário de edição
router.get('/categorias/edit/:id', (req, res) =>{
    Categoria.findOne({_id:req.body.id}).then((categoria)=>{
    res.render('adm/editCategoria', {categoria: categoria})
}).catch((err)=>{
    req.flash('error_msg', 'Essa categoria não existe')
    res.redirect('/adm/categorias')
})




// Formulário de edição para categorias  
<div class="card">
    <div class="card-body">
        <form action="/adm/categorias/nova" method="POST">
            <labFel for="nome">Nome:</label>
            <input type="text" id="nome" name="nome" placeholder="Nome Da Categoria" class="form-control" value="{{categoria.nome}}">
            <label for="slug">Slug:</label>
            <input type="text" id="slug" name="slug" placeholder="Slug Da Categoria" class="form-control" value="{{categoria.slug}}"><br>
           <button type="submit" class="btn btn-success">Confirmar</button>
       </form>
   </div>
</div>
  • 1

    Have to see if on the page (listing of these categories) that calls this page editing you pass a id as a parameter in the url.

  • Then, it looks like the button url: "/Adm/categories/Edit/id:5d6c5e3b528afa0f74d7631a", but when I click appears the error: This category does not exist, error: Casterror: Cast to Objectid failed for value "id:5d6c5e3b528afa0f74d7631a" at path "_id" for model "categories"

  • 1

    You’d have to post the full codes for the problem, just to get an idea.

  • I managed to solve it, I was on the button that carried the link to this form, I’m not very familiar with the language, this project is just to practice. But basically it was a mess, the correct one would be: href='/Adm/categories/Edit/{{_id}' but I put: href='/Adm/categories/Edit/{:id}}' Thank you very much for trying to help :D

  • 1

    Cool that solved, I commented to post the codes because I deduced that probably the error was somewhere in the form, success there!

  • 1

    Simm, thank you. Success there too! By more helpful people in the community.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.