1
My code makes the mistake:
Cannot POST /save-Institution
when I click send in the form
server.js:
const express=require('express');
const path=require('path');
const pages=require('./pages.js');
const server=express();
server
.use(express.urlencoded({extended:true}))
.use(express.static('public'))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'hbs')
.get('/', pages.index)
.get('/adotar', pages.adotar)
.get('/instituicoes', pages.instituicoes)
.get('/instituicao', pages.instituicao)
.get('/cadastrarinsti', pages.cadastrarInst)
.get('/save-instituicao', pages.saveInstituicao);
server.listen(5500, () => {
console.log("Server started");
});
pages js.
const Database=require('./database/db');
const saveInstituicao=require('./database/saveInstituicao');
module.exports={
index(req, res){
return res.render('index');
},
async adotar(req, res){
try{
const db=await Database;
const instituicoes=await db.all("SELECT * FROM adotar");
return res.render("adotar", { adotar });
} catch (error) {
console.log(error);
return res.send('Erro no banco de dados');
}
},
async instituicao(req, res){
const id= req.query.id;
try{
const db=await Database;
const results=await db.all(`SELECT * FROM instituicoes WHERE id="${id}"`);
const instituicao= results[0]
instituicao.images= institiuicao.images.split(",");
instituicao.firstImage=instituicao.images[0];
return res.render("instituicao", { instituicao: instituicao[0] });
} catch (error) {
console.log(error);
return res.send('Erro no banco de dados');
}
},
async instituicoes(req, res){
try{
const db=await Database;
const instituicoes=await db.all("SELECT * FROM instituicoes");
return res.render("instituicoes", { instituicoes });
} catch (error) {
console.log(error);
return res.send('Erro no banco de dados');
}
},
cadastrarInst(req, res){
return res.render('cadastrarinst');
},
async saveInstituicao(req, res){
const fields = req.body;
if(Object.values(fields).includes('')){
return res.send('Todos os campos devem ser preenchidos');}
try {
await saveInstituicao(db,{
name:fields.name,
about:fields.about,
address:fields.address,
telephone:fields.telephone,
images:fields.images.toString(),
opening_hours:fields.opening_hours,
})
return res.redirect('/instituicoes')
} catch (error) {
console.log(error)
return res.send('Erro no banco de dados')
}
}
}
html
<form action="save-institution" method="post">
<fieldset>
<legend>Informações da Instituição</legend>
<div class="input-block">
<label for="name">Nome</label>
<input id="name" name="name" required />
</div>
<div class="input-block">
<label for="about">Sobre <span>Máximo de 300 caracteres</span></label>
<textarea id="about" name="about" required></textarea>
</div>
<div class="input-block">
<label for="telephone">Telefone/Celular</label>
<input id="telephone" name="telephone" required />
</div>
<div class="input-block images">
<label for="images">Fotos</label>
<div class="images-upload" id="images">
<div class="new-upload">
<input name="images" placeholder="Cole o link da foto aqui" required type="url" />
<span onclick="deleteField(event)">
<img src="/images/remove-file.svg" alt="Remover Foto" />
</span>
</div>
</div>
<button onclick="addPhotoField()" type="button">
<img src="/images/add-file.svg" alt="Adicionar foto" />
</button>
</div>
<div class="input-block">
<label for="opening_hours">Horário de Atendimento</label>
<input id="opening_hours" name="opening_hours" required />
</div>
<div>
<button type="submit" class="primary-button">Confirmar</button>
</div>
</fieldset>
</form>
That line
.get('/save-instituicao', pages.saveInstituicao);
, shouldn’t be.post
in place of.get
? And there in his form, theaction
this one withsave-institution
, shouldn’t besave-instituicao
?– Cmte Cardeal
I made the changes and the code entered the catch (error), and in vs appeared "db is not defined"
– Leonardo
Great, so the form works, now about that mistake, I think it’s because of this
await saveInstituicao(db,{
, where, analyzing your code, I think it should be that Voce should writeconst db=await Database;
beforesaveInstituicao
, for thedb
has not yet been defined.– Cmte Cardeal
Error appeared
[Error: SQLITE_ERROR: near ")": syntax error] {
 errno: 1
– Leonardo
Can you update your question so I can see the code like this? It gets a little difficult without seeing the code :)
– Cmte Cardeal
I managed to solve, thank you very much for the help, this last error was just a comma in the wrong place of SQL
– Leonardo
Okay, we’re here to help ;)
– Cmte Cardeal