在 class 中插入函数
Insert a function in a class
我需要将同事的代码集成到我的网站中。我不知道他为什么决定用 class 和异步风格来写数据库,因为我认为没有必要。反正无害:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
class User {
constructor() {
this.user = mongoose.model('user', {
email: String,
password: String,
hash: String,
salt: String,
})
}
checkEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.user.find({email, password}, {}, (err, users) => {
if(err) throw(err)
if(users.length > 0) resolve(users[0])
else reject('not found')
})
})
}
addAccountWOCheck(userInf, way) {
return new Promise((resolve, reject) => {
var collection = new this.user(userInf)
// collection.setPassword(userInf.password)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
})
}
}
现在,我想添加一个 setPassword
加密 password
的函数。我希望该函数由用户实例而不是 class 调用,例如 collection.setPassword(thePassWord)
而不是 addAccountWOCheck
中的 this.user.setPassword(...)
,但我不知道如何插入这个这个 class
的功能
setPassword = function(email) {
var salt = crypto.randomBytes(16).toString('hex');
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'SHA1').toString('hex');
}
有人能帮忙吗?
编辑 1:
我试着在class里面添加:
setPassword(collection) {
collection.hash = "abc";
console.log("setPassword: " + JSON.stringify(collection));
}
并调用 addAccountWOCheck
:
var collection = new this.user(userInf)
this.setPassword(collection)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
我可以看到 setPassword
被调用了,但是 collection
没有在数据库中被修改。
您似乎在寻找 prototype
. Here 是关于如何向现有对象添加更多功能的详细说明。
以下示例可能有所帮助:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
JavaScript Prototypes
All JavaScript objects inherit the properties
and methods from their prototype.
Objects created using an object literal, or with new Object(), inherit
from a prototype called Object.prototype.
Objects created with new Date() inherit the Date.prototype.
The Object.prototype is on the top of the prototype chain.
All JavaScript objects (Date, Array, RegExp, Function, ....) inherit
from the Object.prototype.
我的Edit 1
作品
在class里面添加函数:
setPassword(collection) {
collection.hash = "abc";
console.log("setPassword: " + JSON.stringify(collection));
}
并调用 addAccountWOCheck
:
var collection = new this.user(userInf)
this.setPassword(collection)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
之前没有成功更新collection
的hash
的原因是我忘记在用户模式中添加hash
字段(与我在OP中发布的不同) .
我需要将同事的代码集成到我的网站中。我不知道他为什么决定用 class 和异步风格来写数据库,因为我认为没有必要。反正无害:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
class User {
constructor() {
this.user = mongoose.model('user', {
email: String,
password: String,
hash: String,
salt: String,
})
}
checkEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.user.find({email, password}, {}, (err, users) => {
if(err) throw(err)
if(users.length > 0) resolve(users[0])
else reject('not found')
})
})
}
addAccountWOCheck(userInf, way) {
return new Promise((resolve, reject) => {
var collection = new this.user(userInf)
// collection.setPassword(userInf.password)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
})
}
}
现在,我想添加一个 setPassword
加密 password
的函数。我希望该函数由用户实例而不是 class 调用,例如 collection.setPassword(thePassWord)
而不是 addAccountWOCheck
中的 this.user.setPassword(...)
,但我不知道如何插入这个这个 class
setPassword = function(email) {
var salt = crypto.randomBytes(16).toString('hex');
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'SHA1').toString('hex');
}
有人能帮忙吗?
编辑 1:
我试着在class里面添加:
setPassword(collection) {
collection.hash = "abc";
console.log("setPassword: " + JSON.stringify(collection));
}
并调用 addAccountWOCheck
:
var collection = new this.user(userInf)
this.setPassword(collection)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
我可以看到 setPassword
被调用了,但是 collection
没有在数据库中被修改。
您似乎在寻找 prototype
. Here 是关于如何向现有对象添加更多功能的详细说明。
以下示例可能有所帮助:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
JavaScript Prototypes
All JavaScript objects inherit the properties and methods from their prototype.
Objects created using an object literal, or with new Object(), inherit from a prototype called Object.prototype.
Objects created with new Date() inherit the Date.prototype.
The Object.prototype is on the top of the prototype chain.
All JavaScript objects (Date, Array, RegExp, Function, ....) inherit from the Object.prototype.
我的Edit 1
作品
在class里面添加函数:
setPassword(collection) {
collection.hash = "abc";
console.log("setPassword: " + JSON.stringify(collection));
}
并调用 addAccountWOCheck
:
var collection = new this.user(userInf)
this.setPassword(collection)
collection.save((err, data) => {
if (err) throw (err)
else resolve(data)
})
之前没有成功更新collection
的hash
的原因是我忘记在用户模式中添加hash
字段(与我在OP中发布的不同) .