我对Express尚不陌生,正在尝试创建一个简单的身份验证登录/注册表单。 “ newUser”即将出现,我不确定为什么。。。任何建议都将不胜感激。谢谢!
bcrypt.hash(newUser.password, salt, function(err, hash) {
let express = require("express");
let router = express.Router();
let mongojs = require("mongojs");
let db = mongojs("carapp", ["users"]);
let bcrypt = require("bcryptjs");
let passport = require("passport");
let LocalStrategy = require("passport-local").Strategy;
// login GET PAGE
router.get("/login", function(req, res) {
res.render("login");
});
// Register GET PAGE
router.get("/register", function(req, res) {
res.render("register");
});
// Register POST
router.post("/register", function(req, res) {
let name = req.body.name;
let email = req.body.email;
let username = req.body.username;
let password = req.body.password;
let confirm_password = req.body.confirm_password;
// Validation
req.checkBody("name", "Name field is required").notEmpty();
req.checkBody("email", "Email field is required").notEmpty();
req.checkBody("email", "Please use a Valid Email Address").isEmail();
req.checkBody("username", "Username field is required").notEmpty();
req.checkBody("password", "Password field is required").notEmpty();
req.checkBody("confirm_password", "Passwords Do Not
Match".equals(req.body.password);
// Check For Errors
let errors = req.validationErrors();
if (errors) {
console.log("Form has errors");
res.render("register", {
errors: errors,
name: name,
email: email,
username: username,
password: password,
confirm_password: confirm_password
});
} else {
let newUser = {
name: name,
email: email,
username: username,
password: password
};
}
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
db.users.insert(newUser, function(err, doc) {
if (err) {
res.send(err);
} else {
console.log("User Added. Good JOB using MONGODB Matt!");
req.flash("success", "You are registered and can log in");
res.location("/");
res.redirect("/");
}
});
});
});
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
db.users.findOne({ _id: mongojs.OnjectId(id) }, function(err, user{
done(err, user);
});
});
passport.use(
new LocalStrategy(function(username, password, done) {
db.users.findOne({ username: username }, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: "Incorrect Username" });
}
bcrypt.compare(password, user.password, function(err, isMatch) {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: "Incorrect Password" });
}
});
});
})
);
// Login Post
router.post("/login",passport.authenticate("local",{
successRedirect: "/",
failureRedirect: "/users/login",
failureFlash: "Invalid Username or Password"
} +
function(req, res) {
console.log("Auth Successful");
res.redirect("/");
}
)
);
});
module.exports = router; //brackets needed here
这是我的package.json文件
{
"name": "cars",
"version": "1.0.0",
"description": "car buying application",
"main": "app.js",
"scripts": {
"start" : "nodemon"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"bootstrap": "^4.1.3",
"connect-flash": "^0.1.1",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-messages": "^1.0.1",
"express-session": "^1.15.6",
"mongojs": "^2.6.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"pug": "^2.0.3"
}
}
答案 0 :(得分:1)
def split_votes():
for idx, row in df_ff_reviews.iterrows():
value = ''
s = ''
value = str(row['helpfulness'])
s = value.split('/')
df_ff_reviews['NumHelpfulVotes'] = s[0]
df_ff_reviews['NumVotes'] = s[1]
s[0]): 0
s[1]): 1
s[0]): 19
s[1]): 19
s[0]): 13
s[1]): 13
s[0]): 9
s[1]): 9
s[0]): 3
s[1]): 3
only exists in its block scope。
在let
块中声明它但立即关闭该块时,实际上是一个空操作。
使用您的原始代码:
{}
您可以尝试以下操作:
// newUser = undefined
} else {
let newUser = {
name: name,
email: email,
username: username,
password: password
};
// newUser = Object
}
// newUser = undefined
答案 1 :(得分:0)
确保已安装主体解析器。您的身体可能不是JSON格式。