Sequelize - this.get()

时间:2016-11-22 18:33:36

标签: node.js sequelize.js

我有两个文件:

server.js 用于接收来自客户端的请求:

var db = require("./db.js");
app.post("/users/login", function (req, res) {


    var body = _.pick(req.body, "email", "password");

    db.user.authenticate(body).then(function (data) {   //data is entire record and use "authenticate" function instance

        res.header("Auth", data.generateToken("authentication")).json(data);
    }, function (e) {
        res.status(401).send("Incorrect username or password! Please try again");
    })

user.js的

...........
instanceMethods: {
            generateToken: function (type) {
                if (!_.isString(type)) {
                    return undefined;
                }

                try {
                    //"this" is refer of istance sequelize "user"
                    var stringData = JSON.stringify({ id: this.get("id"), type: type });

                    // encrypt json string "stringData" and we set a secret password for decrypt data
                    var encryptedData = crypto.AES.encrypt(stringData, "yyyy").toString();
                    var token = jwt.sign({
                        token: encryptedData
                    }, "xxxx");

                    return token;

                } catch (e) {
                    console.log(e);
                    return undefined;
                } {

                }
            }
        }

当我从server.js调用data.generateToken时,我使用db.user对象来影响user.js中的所有模块。但是如何知道this.get("id")必须使用data记录的ID? “this”和“data”之间有什么联系?重要提示:“数据”是我控制从用户发送凭据后收到的数据库记录

2 个答案:

答案 0 :(得分:0)

在您的示例中,this指的是定义实例方法的父对象,在本例中是一个Sequelize Instance,它是一个ES6对象。当您致电this.get()时,您正在Instance的特定实例上调用Instance.get(key, options)功能。

获取User.id并将其包含在唯一stringData值中,然后用于生成令牌。

答案 1 :(得分:0)

好的,如果我进入user.js

var user = sequelize.define("user", {  
......

},

classMethods: {  //for create simple methods
            authenticate: function (body) { //pass body object from user.js ( api users/login ) into this class
                return new Promise(function (resolve, reject) { //built a promise for send back the reject or resolve result

                    if (typeof body.email == "string" && typeof body.password == "string") {

                        user.findOne({   //search a specific element into database
                            where: {
                                email: body.email   //where email into database is equals to email that you pass into JSON body
                            }
                        }).then(function (data) {   //return entire record from the database

                            //compareSync permit that compare the original password with password hashed + salt ( from the password_hash column from data table )
                            if (data && bcrypt.compareSync(body.password, data.get("password_hash"))) {
                                console.log("You are logged!");
                                return resolve(data);   
},

instanceMethods: {
            generateToken: function (type) {
                if (!_.isString(type)) {
                    return undefined;
                }

                try {
                    //"this" is refer of istance sequelize "user"
                    var stringData = JSON.stringify({ id: this.get("id"), type: type });
};
return user;

this.get它是指用户变量?然后,在server.js的API中,data是用户变量的一个实例,因此我可以访问generateToken方法并传递data this对象替换?