在我成功创建Categories
表及其create
和index
函数之后。我基本上复制了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)
}