如何解决将模型方法“此”问题归类
我对SWT用户JWT产生了续集的问题,因为我需要创建一个模型方法,然后每当要执行验证时都应用此模型方法。
问题是当我尝试使用“ this”时,返回的是“ user”。
有人可以给我解释为什么以及一些可能的解决方法吗? ty。
const { DataTypes } = require('sequelize')
const sequelize = require('../sequelize/db')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const User = sequelize.define('user',{
id: {
type: DataTypes.STRING,allowNull: false,primaryKey: true
},name: {
type: DataTypes.STRING,require: true,unique: true,validate: {
len: [4,12]
}
},password: {
type: DataTypes.STRING,validate: {
len: {
args: [6,12],msg: 'password between 6 and 12 characters'
},async set(password) {
this.setDataValue('password',await bcrypt.hash(password,8))
}
}
},email: {
type: DataTypes.STRING,validate: {
isEmail: true
}
},avatar: {
type: DataTypes.BLOB,},profile: {
type: DataTypes.TEXT
}
})
User.genAuthToken = async function () {
const user = this
const token = jwt.sign({ _id: user.id },'qweasd',{ expiresIn: '7 days' })
return token
}
const a = async () => {
await User.create({
id: 'asdd',name: 'nishia',email: 'emaia@am.me',password: 'asdasd'
})
const token = await User.genAuthToken()
}
a()
解决方法
您需要一个实例方法,而不是一个类(静态)方法:
$ brew reinstall wget --with-debug
Usage: brew reinstall [options] formula
Uninstall and then install formula using the same options it was originally
installed with,plus any appended brew formula options.
Unless HOMEBREW_NO_INSTALL_CLEANUP is set,brew cleanup will then be run for
the reinstalled formulae or,every 30 days,for all formulae.
-d,--debug If brewing fails,open an interactive
debugging session with access to IRB or a
shell inside the temporary build directory.
-s,--build-from-source Compile formula from source even if a
bottle is available.
-i,--interactive Download and patch formula,then open a
shell. This allows the user to run
./configure --help and otherwise
determine how to turn the software package
into a Homebrew package.
--force-bottle Install from a bottle if it exists for the
current or newest version of macOS,even if
it would not normally be used for
installation.
--keep-tmp Retain the temporary files created during
installation.
-f,--force Install without checking for previously
installed keg-only or non-migrated
versions.
-v,--verbose Print the verification and postinstall
steps.
--display-times Print install times for each formula at the
end of the run.
-q,--quiet Suppress any warnings.
-h,--help Show this message.
Error: invalid option: --with-debug
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。