我目前正在尝试使用 sequelize 为 postgresql 数据库播种,我在我的模型上声明了钩子,在创建单独的记录(例如测试)时可以正常工作
我的种子文件中的数据是否需要在最终表格中显示,或者我可以在创建时调用钩子吗?
这是我的文件:
/*users-seed.js*/
'use strict'
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert('Users', [/*users-data*/])
},
down: function (queryInterface, Sequelize) {
return queryInterface.bulkDelete('Users', null, {})
}
}
Run Code Online (Sandbox Code Playgroud)
和
/*user.js*/
module.exports = function (sequelize, DataTypes) {
let User = sequelize.define('User', {
/* user attributes */
}, {
instanceMethods: {
hashPassword: function (password) {
return bcrypt.hash(password, 15)
},
hashEmail: function (email) {
return crypto.createHash('sha256').update(email).digest('hex')
}
},
hooks: {
beforeCreate: function (user) {
return user.hashPassword(user.password_digest).then(function (hashedPassword) { …Run Code Online (Sandbox Code Playgroud) 我正在使用node.js v6.7.0并且在声明一个带有'this'的引用的对象时,如果它在一个箭头函数内,则返回undefined但是当它在一个常规的匿名函数中时它返回该对象本身(这就是我想)
例如
let obj = {
key: 'val',
getScopeWithArrow: () => {return this;}, //returns undefined
getScopeWithAnonymous: function() {return this;} //returns the object properly
}Run Code Online (Sandbox Code Playgroud)