Populating select with data sent from controller and trying to save to BD

Asked

Viewed 26 times

0

I am sending to form an array to popular select so,

controller that sends to the view:

const novaForm = async ({ Maquina }, req, res) => {
const maquina = await Maquina.find({status: 'livre'})
res.render('operacoes/nova', { maquina })
}

View:

<select name="maquina" class="custom-select" id="maquina">
<option selected>máquina</option>
<% maquina.forEach( maq => { %>
<option value="<%= maq.nome %>"><%= maq.nome %></option>
<% }) %>
</select>

Controller that saves:

const novaProcess = async ({ Operacao }, req, res) => {
console.log(req.body)
const operacao = new Operacao(req.body)
try{
    await operacao.save() 
    res.redirect('/operacoes')
} catch (e) {
    res.render('operacoes/nova', {errors: Object.keys(e.errors)})
 }
}

But when I order to save me is returned the following error:

45    <select name="maquina" class="custom-select" id="maquina">
46                           <option selected>máquina</option>
47                           <% maquina.forEach( maq => { %>
48                           <option value="<%= maq.nome %>"><%= maq.nome %></option>
49|                           <% }) %>
50|                         </select>

maquina is not defined

It popula certinho o select, no error, the error occurs only when ordering save.

  • Put a console.log of the operation variable after it takes the data from req.body and tells me here how it returns

  • Look at the return of friend operation... { _id: 5c9bebd72dc5bb196cb7027d, machine: '5c9793558a7fae2e6c6dbb0b' }

No answers

Browser other questions tagged

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