Save Array to Mysql using Nodejs

Asked

Viewed 24 times

0

I have a form I’m making with Vuejs and Nodejs to the Back-End.

I have a checkbox that gives me a value in Array, and wanted to save these values in the database, and when I try to save, it error obviously, for having to be String, and not Array.

I can’t think of a way to transform the value of these checkboxs selected in String to pass to the database.

const db = require('./db')

const Orcamentos = db.sequelize.define('orcamentos',{
    nome: {
        type: db.Sequelize.STRING
    },
    cpf: {
        type: db.Sequelize.STRING
    },
    celular: {
        type: db.Sequelize.STRING
    },
    email: {
        type: db.Sequelize.STRING
    },
    ramo: {
        type: db.Sequelize.STRING
    },
    empresa: {
        type: db.Sequelize.STRING
    },
    templates: {
        type: db.Sequelize.ARRAY(Sequelize.STRING),
    },
    sugestao: {
        type: db.Sequelize.STRING
    }
})

//Comando para criar a tabela. DEIXAR COMENTADO.
       Orcamentos.sync({force: true})

module.exports = Orcamentos
const express = require('express')
const path = require('path')
const app = express()
const history = require('connect-history-api-fallback');
const Orcamentos = require('./models/Orcamentos')

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

//Definindo a rota.
const staticFileMiddleware = express.static(path.join(__dirname + '/dist'));

app.use(staticFileMiddleware);
app.use(history({
  disableDotRule: true,
  verbose: true
}));
app.use(staticFileMiddleware);

app.route('/*')
  .get(function (req, res) {
    res.sendFile(path.join(__dirname + '/dist/index.html'));
  });



app.post('/sucesso', function (req, res) {
  
  Orcamentos.create({
    
    nome: req.body.nome,
    cpf: req.body.cpf,
    celular: req.body.celular,
    email: req.body.email,
    ramo: req.body.ramo,
    empresa: req.body.empresa,
    templates: req.body.templates,
    sugestao: req.body.sugestao


  })
  
  .then(function(){
    res.redirect('/sucesso')
    res.send("Formulário enviado com sucesso!")
  }).catch(function(erro){
    res.send("Houve um erro."+erro)
  })
})


module.exports = app;

const server = app.listen(process.env.PORT || 8080, function () {
  const port = server.address().port;
  console.log("Servidor rodando na porta => https://localhost:", port);
});
  • Reading the Sequelize documentation, I saw that it doesn’t let me transform to String using the mysql database, only Postgresql.

No answers

Browser other questions tagged

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