我是sequelize的新手,每次创建一个新的" Rider"我都试图将这个名字的第一个字母大写。所以它看起来像我的桌子。我还没能做到:
这是我的模特:
const db = require("./db");
const Sequelize = require("sequelize");
//(w / WSL ranking, Last tournament won, Country, favorite wave, current board).
const Rider = db.define("rider", {
name: {
type: Sequelize.STRING,
allowNull: false
},
country: Sequelize.STRING,
wsa: {
type: Sequelize.INTEGER,
allowNull: false
},
currentBoard: {
type: Sequelize.STRING,
allowNull: false
},
favWave: Sequelize.STRING,
lastTournamentWon: Sequelize.STRING,
img: {
type: Sequelize.TEXT,
defaultValue:
"no_found.png"
}
});
Rider.beforeCreate = () => {
return this.name[0].toUpperCase() + this.name.slice(1);
}
module.exports = Rider;
当我创建一个新行时,这个名称并没有大写,我还没有找到原因?我是否必须将实例和回调函数作为我的钩子的参数传递?
答案 0 :(得分:0)
如@iwaduarte的评论所述,您需要传递实例,如下所示
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
User.hook('beforeCreate', (user, options) => {
user.username = user.username.charAt(0).toUpperCase() + user.username.slice(1);
});
sequelize.sync({ force: true })
.then(() => User.create({
username: 'one',
}).then((user) => {
console.log(user.username);
})
);