UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):TypeError:密码短语必须是缓冲区

时间:2018-03-13 06:00:11

标签: javascript node.js mongodb express passport.js

这是我的代码,但是我收到消息:UnhandledPromiseRejectionWarning:未处理的promise promise(拒绝ID:2):TypeError:密码短语必须是缓冲区 每当我输入我的护照输入并使用我在创建新用户的“/ registrar”下的发布请求时,似乎无法找到它会抛出错误的原因,尝试使用硬编码数据而不是req.body。等等但仍然有同样的错误。救命啊!

var express               = require("express"),

mongoose              = require("mongoose"),
passport              = require("passport"),
bodyParser            = require("body-parser"),
User                  = require("./models/user"),
LocalStrategy         = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose");


mongoose.connect("mongodb://localhost/ommbc_app");
var app = express();
// app.set("view engine", "ejs");
app.use(express.static(__dirname + '/views/assets/css'));
app.use(express.static(__dirname + '/views/assets/js'));
app.use(express.static(__dirname+'/views/assets'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(require("express-session")({
    secret: "OMMBC PRRO",
    resave: false,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());

passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());


///ROUTES
app.get("/", function(req,res){
    res.sendFile(__dirname+"/views/login.html")
});

app.get("/login", function(req,res){
    res.sendFile(__dirname+"/views/login.html")
});

app.get("/problemas",isLoggedIn, function(req,res){
    res.render("/");
});

//AUTH ROUTES
app.get("/registrar", function(req,res){
    res.sendFile(__dirname+'/views/signup.html');
});

//handling user sign up
app.post("/registrar",function(req,res){
    // console.log(req.body.username);
    // console.log(req.body.password);
    // req.body.username
    // req.body.password
    User.register(new User(
        {
            username: req.body.username,
            password: req.body.password
        }),function(err,user){
        if(err){
            console.log('HEllo');
            console.log(err);
            return res.sendFile("/login");
        }
        // else{
        //     console.log("LOGGEDIN ");
        //     passport.authenticate("local")(req,res,function(){
        //         res.redirect("/problemas");
        //     });
        // }
    }
    );
});

//LOGIN ROUTES
// app.get("/login", function(req,res){
//     res.sendFile(__dirname+"/views/login")
// });

app.post("/login",passport.authenticate("local",{
    successRedirect: "/problemas",
    failureRedirect: "/login"
}) ,function(req,res){
});

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Server Started...");
});

//MIDDLEWARE
app.get("/logout",function(req,res){
    req.logout();
    res.redirect("/")
});


function isLoggedIn(req,res,next){
    if(req.isAuthenticated()){
        return next();
    }
    else{
        res.redirect("/login");
    }
}

1 个答案:

答案 0 :(得分:0)

有点发现问题,因为我可以将User的数据发送到数据库,但我的主要问题是我试图在没有散列的情况下发送密码,因为将来应该是API而不是db。

var mongoose = require(" mongoose"); var passportLocalMongoose = require(" passport-local-mongoose"); var UserSchema = new mongoose.Schema({nombre:String,password:String,}); UserSchema.plugin(passportLocalMongoose); module.exports = mongoose.model(" User",UserSchema);