Expressjs - Sequelize - Data type not recognized

Asked

Viewed 394 times

0

I manually created an sql database and now I’m trying to configure my sequencing templates to match the columns in my sql tables, but I found a problem with my model related to an unrecognized field type. I cannot identify the error and if it is related not to match the data type of a field in my database or if the sequence does not recognize part of my code.

this is the mistake:

C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_modules\sequelize\lib\model
.js:1005
    throw new Error(`Unrecognized datatype for attribute 
"${this.name}.${name}"`);
    ^

Error: Unrecognized datatype for attribute "postagens.titulo"
at rawAttributes._.mapValues 
(C:\Users\Fabricio\Desktop\programação\node-pratica\project
1\node_modules\sequelize\lib\model.js:1005:15)
at C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_modules\lodash\lodas
h.js:13401:38
at C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_modules\lodash\lodas
h.js:4905:15
at baseForOwn (C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_modules\
lodash\lodash.js:2990:24)
at Function.mapValues (C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_
modules\lodash\lodash.js:13400:7)
at Function.init (C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_modul
es\sequelize\lib\model.js:1001:28)
at Sequelize.define (C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\node_mo
dules\sequelize\lib\sequelize.js:426:11)
at Object.<anonymous> (C:\Users\Fabricio\Desktop\programação\node- 
pratica\project1\model
s\post.js:3:27)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js 
(internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

This is my code:

const db = require('./db')

const Post = db.sequelize.define('postagens', {
titulo: {
    type: db.Sequelize.string 
},
conteudo: {
    type: db.Sequelize.text
}
})

module.exports = Post

Post.sync({force:true})

modulo 'db':

const Sequelize = require('sequelize')

// Conexão com o banco de dados

const sequelize = new Sequelize('postapp', 'root', 'frajola10', {
    host: 'localhost',
    dialect: 'mysql'
})

// Exportar o modulo

module.exports = {
    Sequelize: Sequelize,
    sequelize: sequelize
}

index.js file':

// Chamada dos modulos da aplicação

const express = require('express')
const app = express()
const handlebars = require('express-handlebars')
const bodyParser = require('body-parser')

// Config

// Template Engine

    app.engine('handlebars', handlebars({defaultLayout: 'main'}))
    app.set('view engine', 'handlebars')

// Body Parser

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

// Rotas

// '.get' cria uma rota do tipo get

app.get('/cad', function(req,res){

// 'render' renderiza o arquivo handlebars dentro da pasta views('form')

    res.render('form')
})

// '.post' cria uma rota do tipo post

app.post('/add', function(req,res){

// 'req' faz uma requisão do conteudo do post presente no local 
// indicado(body.titulo)

    res.send('texto: ' + req.body.titulo + '<br>' + ' conteudo: ' + 
    req.body.conteudo)
 })

// Criação do servidor local

  app.listen(8080, function(){
  console.log('Rodando!!')
  })

form handlebars 'form.handlebars':

<form action="/add" method="post">
   <p>Titulo</p>
   <input type="text" name="titulo">
   <p>Conteudo</p>
   <textarea name="conteudo"></textarea>
   <br>
   <button>Cadastrar</button>
</form>

file 'main.handlebars':

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    {{{body}}}
</body>
</html> 

Could you answer me what’s going on?

  • Face only with that posted has no way of knowing, post the form Html that sends the data and the Js file that manages the post.

  • It gave in the same, without form html and js that contain the routes and what should happen when access to each route has no way anyone knows what is happening!

  • It is, if your code on index js. it’s just that, it can’t really work, it has no method create() or update() linking the template to Html.

  • I already have a database created

  • You have a bank, you have the connection to the bank and you have a model, just you are not doing anything with the model, ta missing connect the html elements (title input and content) with the seat through the model.

1 answer

0


I figured it out, Post.js':

const db = require('./db')

const Post = db. sequelize.define('postagens', { 
titulo: {
    type: db.Sequelize.STRING
    },
    conteudo:{
     type: db.Sequelize.TEXT}
    })

Post.sync({force:true})

Browser other questions tagged

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