Reputation: 353
I am developing a project in which admins can add chat rooms and those have five fields: (id, creator (admin who created it), name, slug and createdAt) and I am receiving this error Unhandled rejection SequelizeValidationError: notNull Violation: name cannot be null.
//models/user.js
var Sequelize = require('sequelize');
var passportLocalSequelize = require('passport-local-sequelize');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var mydb = new Sequelize(config.database, config.username, config.password, config);
// We do not need additional features for our users, so we just use the default user model
// provided by Passport
var User = passportLocalSequelize.defineUser(mydb,
{
email: { type: Sequelize.STRING, allowNull: false, unique: true }
},{}
);
//Function at a prototype level for performance optimization
User.Instance.prototype.$Model.findByEmail = function(email, done){
var parameters = {};
parameters.email = email;
var user = this.findOne({where: parameters});
user.then(function(usr) {
done(null, usr);
});
}
mydb.authenticate().then(function(done)
{
if(mydb.sync({force: false}))
done();
}).catch(function(error,user)
{
console.log(error);
});
module.exports = User;
//models/chat.js
var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var User = require('./user');
var mydb = new Sequelize(config.database, config.username, config.password, config);
var Chat = mydb.define('Chat',
{
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: Sequelize.STRING, unique: true, allowNull: false,
validate: {
notNull: { args: true, msg: "You must enter a name" }
},
set: function(val) {
val = val.toLowerCase().trim();
this.setDataValue('title', val );
this.setDataValue('slug', val.split(" ").join("_"));
}
},
slug: { type: Sequelize.STRING, unique: true, allowNull: false },
creator: { type: Sequelize.INTEGER, references: { model: User, key: 'id' }}
},
{
timestamps: true,
updatedAt: false // I only want the field createdAt
}
)
mydb.authenticate().then(function(done){
if(mydb.sync({force: false}))
done()
}).catch(function(err, chat){
console.log(err);
});
module.exports = Chat;
//routes/admin.js
router.post("/dashboard",isAuthenticated, function(req,res){
if (req.body.chatName) {
try{
console.log(req.body.chatName);
console.log(Chat.create({ name: req.body.chatName, creator: req.user.id }));
}
catch (e){
console.log(e.message);
}
}
});
I only copied and pasted the function which manages the chat rooms creation, console.log(req.body.chatName); prints correctly the entered name
Upvotes: 2
Views: 18920
Reputation: 56
From the sequelize docs:
// setting allowNull to false will add NOT NULL to the column, which means an error will be
// thrown from the DB when the query is executed if the column is null
In your Chat table you set
name: { type: Sequelize.STRING, unique: true, allowNull: false,
validate: {
notNull: { args: true, msg: "You must enter a name" }
},
set: function(val) {
val = val.toLowerCase().trim();
this.setDataValue('title', val );
this.setDataValue('slug', val.split(" ").join("_"));
}
},
for the user field, where allowNull is false.
So the error you're getting is directly from the db instead of your message in the validate property. Get rid of the allowNull property, keep the validation, and you should be good. A little late, but hope this clears things up!
Upvotes: 4