【问题标题】:Insert a function in a class在类中插入函数
【发布时间】:2018-04-24 18:09:45
【问题描述】:

我需要将同事的代码集成到我的网站中。我不知道他为什么决定以类和异步的方式编写数据库,因为我认为没有必要。反正是无害的:

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)
            })
        })
    }
}

现在,我想添加一个加密passwordsetPassword 函数。我希望该函数由用户实例而不是类调用,例如collection.setPassword(thePassWord) 而不是addAccountWOCheck 中的this.user.setPassword(...),但我不知道如何将此函数插入​​此类

setPassword = function(email) {
    var salt = crypto.randomBytes(16).toString('hex');
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'SHA1').toString('hex');
}

有人可以帮忙吗?

编辑 1:

我尝试在类内添加:

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在数据库中没有被修改。

【问题讨论】:

  • “他”是谁? ;)
  • 为什么不直接添加与现有方法checkEmailPasswordaddAccountWOCheck 语法完全相同的方法呢?你试过吗?你问过写这门课的同事吗?
  • 因为每次修改用户之前,都需要先找到用户。但是,我想直接在用户身上工作。
  • @SoftTimur 是的 - 这些其他方法也适用于 User instances。它们不是static 方法。我不确定您所说的“它需要先找到用户”是什么意思——在实例上调用方法时,您不是已经有用户了吗?
  • 我希望它可以在编辑 1 中工作。虽然类设计几乎没用。 this 状态无关紧要,这些应该是静态方法。此外,save 返回一个承诺,不需要被承诺。

标签: javascript express mongoose ecmascript-6 mongoose-schema


【解决方案1】:

我的Edit 1 工作

在类中添加函数:

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)
})

之前没有成功更新collectionhash 的原因是我忘记在用户架构中添加hash 字段(与我在OP 中发布的不同)。

【讨论】:

    【解决方案2】:

    似乎您正在寻找 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 原型

    所有 JavaScript 对象都继承属性 以及原型中的方法。

    使用对象字面量或使用 new Object() 创建的对象继承 来自一个名为 Object.prototype 的原型。

    使用 new Date() 创建的对象继承 Date.prototype。

    Object.prototype 位于原型链的顶端。

    所有 JavaScript 对象(日期、数组、正则表达式、函数......)都继承 来自 Object.prototype。

    【讨论】:

    • 原型是在现有的 JS 对象上实现的,请查看我刚刚在答案部分更新的 JavaScript Prototypes 块。
    • 对不起,你能具体告诉我如何处理我的collectionsetPassword吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2014-11-04
    • 2015-07-22
    相关资源
    最近更新 更多