Reputation: 75
Does anyone know how to solve this? There is something I am not seeing. To see if the controller was working, I returned the response receiving the request in json, it worked, there was no error.
The problem seems to be with the controller, but the controller...
Edit
Controller
import Post from '../models/Post';
import * as Yup from 'yup';
class PostController {
async store(req, res) {
try {
//Checks if the fields have been filled correctly
const schema = Yup.object().shape({
title: Yup.string()
.required(),
content: Yup.string()
.required()
});
if(!(await schema.isValid(req.body))) {
return res.status(400).json({ error: 'Error 1' });
}
//Abstraction of fields
const { title, content } = req.body;
const createPost = await Post.create(title, content);
if(!createPost) {
return res.json({error: 'Error 2'})
}
//If everything is correct, the information will be registered and returned.
return res.json({
title,
content,
});
}
catch (err) {
console.log("Error: " + err);
return res.status(400).json({error: 'Error 3'});
}
}
}
export default new PostController();
Model:
import Sequelize, { Model } from 'sequelize';
class Post extends Model {
static init(sequelize) {
//Fields registered by the user
super.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
title: Sequelize.STRING,
content: Sequelize.TEXT,
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updated_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
},
{
sequelize,
tableName: 'posts'
});
return this;
}
}
export default Post;
Migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('posts', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
content: {
type: Sequelize.TEXT,
allowNull: false,
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
}
});
},
down: (queryInterface) => {
return queryInterface.dropTable('posts');
}
};
The terminal error:
TypeError: Cannot read property 'length' of undefined
Upvotes: 3
Views: 10002
Reputation: 1
In my case i have error in syntax of ENUM in my schema. When i corrected the syntax, the issue resolved.
Upvotes: 0
Reputation: 11
I had the same problem that Naruto Uzumaki. I forgot to put the connection with the model on the file that initialize the models.
Upvotes: 1
Reputation: 622
I was having the same problem
The problem with my code was that I was forgetting to add the module name to my database's index.js file inside my models array
eg: const **models** = [User,**Task**]
I found the answer on this post: https://github.com/sequelize/sequelize/issues/11111#issuecomment-697078832
Upvotes: 1
Reputation: 1889
You need to define all columns that you put on migration inside your model. As you put allowNull: false
, the database needs the column info.
I believe that you can solve adding the columns that you declared on your migration
inside your Model
and, if you don't wanna to declare those columns on your Controller
, add the defaultValue
property on those columns.
This will allow to sequelize to insert the appropriate data on those columns. As example: defaultValue: Sequelize.NOW
on created_at
column.
Another thing that you need to put is the table name, like this (this is one Model that I use inside one of my projects:
static init(sequelize) {
super.init(
{
categoryId: {
type: Sequelize.INTEGER,
primaryKey: true,
field: 'id',
},
dateUpdated: {
type: Sequelize.DATE,
field: 'updated_at',
defaultValue: Sequelize.NOW,
},
// ... other fields here,
},
{
sequelize,
tableName: 'categories',
}
);
}
// ...
}
export default Category;
And, therefore, try to import as Instance, not as a class.
EDIT 1:
Other thing. The error that are in your answer is on line 140 of file lib/model.js
(see here on github on the master repo of sequelize).
Looking at this, try to declare your primary key on model if if you didn't yet.
EDIT 2: In your controller, try this (according to docs):
await Post.create({ title, content });
Try to pass an json object with info that you want to store and not as parameters.
EDIT 3:
You need to import database.js
before call the controllers, I had problems in this point (follow a database.js
that is working):
// Imports here
import Category from '../app/models/Category';
//...
const models = [
// Models that I want to use
Category,
//...
];
class Database {
constructor() {
this.init();
}
// Load on database init
init() {
this.connection = new Sequelize(databaseConfig);
models.forEach(model => model.init(this.connection));
}
}
export default new Database();
Upvotes: 4