Error listing posts with Node.js and sequelize

Asked

Viewed 178 times

0

Every time I open the route "/" says finAll is not a function, what is wrong?

const express = require('express')
const app = express()
const handlebars = require('express-handlebars')
const bodyParser = require('body-parser')
const sequelize = require('sequelize')
const Post = require('./models/Post')

    // handlebars
    app.engine('handlebars', handlebars( { defaultLayout: 'main' } ))
    app.set('view engine', 'handlebars')
    // body-parser
    app.use(bodyParser.urlencoded( { extended: false } ))
    app.use(bodyParser.json())
    // rotas
    app.get("/", (req, res) => {
            Post.findAll().then( (posts) => {
                    res.render("pages/home", {posts: posts});
            });
    });

    app.get("/postar", (req, res) => {
            res.render('pages/form')
    })

    app.post('/send', (req, res) => {
            Post.create({
                    titulo: req.body.titulo,
                    conteudo: req.body.conteudo
            }).then( () => {
                    res.redirect("/")
            }).catch( (err) => {
                    res.send("Houve um erro: " + err)
            })
    })

    const PORT = 8080
    app.listen(PORT, () => {
            console.log("servidor rodando na porta " + PORT)
    })

Content of models/db.js:

const Sequelize = require('sequelize')
const sequelize = new Sequelize("friendship", "root", "", {
  host: "localhost",
  dialect: "mysql"
});

// Postagem.sync( { force: true } )

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

Content of models/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 } )
  • Can inform the code of your file ./models/Post?

  • https://pastebin.com/UB0TZ7Z0

  • https://pastebin.com/juMZU3aS

  • Do not put to Pastebin. Add to your question, otherwise it is incomplete and runs the risk of being closed due to lack of information

  • It’s just that I lent my network adapter to my friend just for today and the internet isn’t picking up, and then I’m using the stack by cell phone and I can’t format texts :/

  • No problem, paste in the answer I format

  • But can you tell which error? I guarantee nn is the internet, because I installed all the necessary packages

  • All right, hold on

  • const Sequelize = require('sequelize') const sequelize = new Sequelize( "Friendship", "root", ", { host: "localhost", dialect: "mysql" }) // Post.Sync( { force: true } ) module.Exports = { Sequelize: Sequelize, sequelize: sequelize } ~

  • const db = require('. /db') const Post = db.sequelize.define("posts", { title: { type: db.Sequelize.STRING }, content: { type: db.Sequelize.TEXT } // Post.Sync( { force: true } )

  • Edit the question and stick to it

  • it’s all going together I don’t know why, want to wait until tomorrow until I get my adapter?

  • wait a second

  • There you go, there you go

Show 9 more comments

1 answer

0

You did not export the module inside the file Post. At the end of your file add the following line:

module.exports = Post;

Browser other questions tagged

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