如何使用node.js在postgres DB中保存用户 - TypeError:done不是函数

时间:2017-04-05 20:16:21

标签: node.js postgresql sequelize.js passport.js

我尝试注册用户。当试图将关于他的信息保存到数据库中时(使用PostgreSQL)我得到的错误不是函数。

如果我需要添加更多代码,请不要犹豫。

如果您对node.js和postgres的用户注册有一个很好的教程,请将它链接到我。

感谢您的帮助:D

完整错误:

Executing (default): SELECT "id", "name", "email", "username", "password", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."username" = NULL LIMIT 1;
Executing (default): INSERT INTO "Users" ("id","name","email","username","password","createdAt","updatedAt") VALUES (DEFAULT,'Domen','*****@gmail.com','Domen123','Domen123','2017-04-05 20:06:01.069 +00:00','2017-04-05 20:06:01.069 +00:00') RETURNING *;
C:\Users\ASUS\Desktop\*****\models\user.js:26
            done(null, user);
            ^

TypeError: done is not a function
    at C:\Users\ASUS\Desktop\*****\models\user.js:26:13
    at C:\Users\ASUS\Desktop\*****\node_modules\bcrypt-nodejs\bCrypt.js:631:3
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Passport.js

var passport = require('passport')
    , LocalStrategy = require('passport-local').Strategy
    , db = require('../models')

// Serialize Sessions
passport.serializeUser(function(user, done){
    done(null, user);
});

//Deserialize Sessions
passport.deserializeUser(function(user, done){
    db.User.find({where: {id: user.id}}).success(function(user){
        done(null, user);
    }).error(function(err){
        done(err, null)
    });
});

// For Authentication Purposes
passport.use(new LocalStrategy(
    function(username, password, done){
        db.User.find({where: {username: username}}).success(function(user){
            passwd = user ? user.password : ''
            isMatch = db.User.validPassword(password, passwd, done, user)
        });
    }
));

user.js的

var bcrypt = require('bcrypt-nodejs')

module.exports = function (sequelize, DataTypes) {
    var User = sequelize.define('User', {
            name: {type: DataTypes.STRING},
            email: {type: DataTypes.STRING, unique: true},
            username: {type: DataTypes.STRING, unique: true},
            password: {type: DataTypes.STRING}
        }
        , {
            instanceMethods: {
                generateHash: function (password, done) {
                    bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
                        bcrypt.hash(password, salt, null, done);
                    });
                },
                validPassword: function (password, next) {
                    bcrypt.compare(password, this.password, next)
                }
            }
        })
    User.beforeCreate(function (user, done) {
        user.generateHash(user.password, function (err, encrypted) {
            if (err) return done(err);
            user.password = encrypted;
            done(null, user);
        })
    })
    return User;

}

依赖

"devDependencies": {
    "body-parser": "^1.14.1",
    "cookie-parser": "^1.4.0",
    "express": "^4.13.3",
    "express-handlebars": "^2.0.1",
    "express-session": "^1.12.1",
    "formidable": "^1.0.17",
    "parseurl": "^1.3.0"
  },
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "connect-flash": "^0.1.1",
    "helmet": "^3.5.0",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "pg": "^6.1.5",
    "sequelize": "^3.30.4"
  }

1 个答案:

答案 0 :(得分:0)

User.beforeCreate有3个参数模型,选项和回调函数,这是在你的情况下完成所以写这个:

User.beforeCreate(function (user, options, done)