SequelizeJS无法找到表

时间:2018-07-14 17:57:56

标签: node.js express sequelize.js

在我成功创建Categories表及其createindex函数之后。我基本上复制了Posts表的所有内容。尽管服务器没有返回错误。 try and catch给我输出:

{"error":"SequelizeDatabaseError: SQLITE_ERROR: no such table: main.undefined"}

这是我的Post模型:

const User = require('./User')
const Category = require('./Category')

module.exports = (sequelize, DataTypes) => {
  const Post = sequelize.define('Post', {
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    body: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    user_id: {
      type: DataTypes.INTEGER,
      references: {
        model: User,
        key: 'id'
      },
      allowNull: true
    },
    category_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Category,
        key: 'id'
      },
      allowNull: true
    }
  })

  return Post
}

这是我的PostController.js

const {Post} = require('../models')

module.exports = {
  async index (req, res) {
    try {
      const posts = await Post.findAll({
        limit: 200
      })
      res.send(posts)
    } catch (err) {
      res.status(500).send({
        error: err
      })
    }
  },
  async create (req, res) {
    try {
      const post = await Post.create(req.body)
      res.send(post)
    } catch (err) {
      res.status(500).send({
        error: err
      })
    }
  }
}

这里是routes.js

const PostController = require('./controllers/PostController')
const CategoryController = require('./controllers/CategoryController')

module.exports = (app) => {
  // POST CONTROLS
  app.get('/posts', PostController.index)
  app.post('/post', PostController.create)
  // CATEGORY CONTROLS
  app.get('/categories', CategoryController.index)
  app.post('/category', CategoryController.create)
}

0 个答案:

没有答案