无法读取未定义的postgres节点上的属性findAll

时间:2019-01-25 04:30:58

标签: javascript node.js postgresql

我遇到以下错误

何时

卷曲-XGET http://localhost:5000/api/books

  

TypeError:无法读取未定义的属性'findAll'
  在   /Users/eli/nodework/elifull3/app/route/book.route.js:11:17

我引用了这个

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

但是我不认为这与es2015编写JavaScript的方式有关。

我想我一切都设置正确。

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}!`),
);

0 个答案:

没有答案