1

I'm getting the following error

when

curl -XGET http://localhost:5000/api/books

TypeError: Cannot read property 'findAll' of undefined
   at /Users/eli/nodework/elifull3/app/route/book.route.js:11:17

I referenced this

TypeError: Cannot read property 'findAll' of undefined (expressjs)

However i don't think this is relevant to the es2015 way of writing javascript.

I think i have everything set up correctly.

db.config.js

import dotenv from 'dotenv';
import Sequelize from 'sequelize';

const env = dotenv.config();

const ourSequelize = new Sequelize(process.env.DATABASE, process.env.DATABASE_USER, process.env.DATABASE_PASS, {
  host: 'localhost',
  dialect: 'postgres',
  pool: {
    max: 10,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
});


const db  = {}


db.sequelize = ourSequelize;
db.Sequelize = Sequelize;

module.exports = db

book.model

import Sequelize from 'sequelize';
import db from '../config/db.config';

module.exports = function(Sequelize, DataTypes) {
    const Book = Sequelize.define('book', {
      title: {
        type: DataTypes.STRING
      },
      author: {
        type: DataTypes.STRING
      },
      description: {
        type: DataTypes.STRING
      },
      published: {
          type: DataTypes.INTEGER
      }
    });

    return Book;

};

book.route

// import books from '../controller/book.controller.js';
import express from 'express';
import db from '../config/db.config';
import models from '../model/book.model';
import Sequelize from 'sequelize';

const router = express.Router();


router.get('/', (req, res) => {
    models.Book.findAll()
        .then(books =>{
            console.log(books);
            res.sendStatus(200);
        })
        .catch(err => console.log(err));

});
module.exports = router;

App.js

import express from 'express';
import bodyParser from 'body-parser';
import logger from 'morgan';
import dotenv from 'dotenv';
import db from './app/config/db.config.js';
import book from'./app/route/book.route';

const app = express();

const env = dotenv.config();

const PORT = process.env.PORT;

app.use(bodyParser.json())
app.use(logger('dev'));
app.use('/api/books', book);


const cors = require('cors')
const corsOptions = {
  origin: 'http://localhost:5000',
  optionsSuccessStatus: 200
}
app.use(cors(corsOptions))

// force: true will drop the table if it already exists
db.sequelize.sync({force: true}).then(() => {
  console.log('Drop and Resync with { force: true }');
});



// Create a Server

app.listen(PORT, () =>
  console.log(`Example app listening on port ${PORT}!`),
);
randal
  • 1,272
  • 3
  • 23
  • 48

0 Answers0