I am trying to create a user registration with a profile picture upload input field. This is the file I am working on user.js. The error is coming for this line 31: if (password.length < 6) {
const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");
const passport = require("passport");
const multer = require('multer');
const path = require('path');
// Load User model
const User = require("../models/User");
const {forwardAuthenticated} = require("../config/auth");
// Login Page
router.get("/login", forwardAuthenticated, (req, res) => {
res.render("login", {title: "Login", layout: "layout"});
});
// Register Page
router.get("/register", forwardAuthenticated, (req, res) => {
res.render("register", {title: "Register", layout: "layout"});
});
// Register
router.post("/register", (req, res) => {
const {name, email, password, password2} = req.body;
let errors = [];
if (!name || !email || !password || !password2) {
errors.push({msg: "Please enter all fields"});
}
if (password.length < 6) {
errors.push({msg: "Password must be at least 6 characters"});
}
if (password != password2) {
errors.push({msg: "Passwords do not match"});
}
if (errors.length > 0) {
res.render("register", {
errors,
name,
email,
password,
password2,
title: "Register",
layout: "Layout",
});
} else {
User.findOne({email: email}).then((user) => {
if (user) {
errors.push({msg: "Email already exists"});
res.render("register", {
errors,
name,
email,
password,
password2,
title: "Register",
layout: "Layout",
});
} else {
const newUser = new User({
name,
email,
password,
filepath
});
//Set The Storage Engine
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + newUser._id + path.extname(file.originalname));
}
});
// Init Upload
const upload = multer({
storage: storage,
limits: {fileSize: 1000000},
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
}
}).single('avatar');
// Check File Type
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb('Error: Images Only!');
}
}
newUser.filepath = storage;
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then((user) => {
req.flash(
"success_msg",
"You are now registered and can log in"
);
res.redirect("/users/login");
})
.catch((err) => console.log(err));
});
});
}
});
}
});
// Login
router.post("/login", (req, res, next) => {
passport.authenticate("local", {
successRedirect: "/dashboard",
failureRedirect: "/users/login",
failureFlash: true,
})(req, res, next);
});
// Logout
router.get("/logout", (req, res) => {
req.logout();
req.flash("success_msg", "You are logged out");
res.redirect("/users/login");
});
module.exports = router;
Error:
TypeError: Cannot read property 'length' of undefined at D:\temp_project\smpms\routes\users.js:31:18 at Layer.handle [as handle_request] (D:\temp_project\smpms\node_modules\express\lib\router\layer.js:95:5) at next (D:\temp_project\smpms\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\temp_project\smpms\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\temp_project\smpms\node_modules\express\lib\router\layer.js:95:5) at D:\temp_project\smpms\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\temp_project\smpms\node_modules\express\lib\router\index.js:335:12) at next (D:\temp_project\smpms\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\temp_project\smpms\node_modules\express\lib\router\index.js:174:3) at router (D:\temp_project\smpms\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (D:\temp_project\smpms\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (D:\temp_project\smpms\node_modules\express\lib\router\index.js:317:13) at D:\temp_project\smpms\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:\temp_project\smpms\node_modules\express\lib\router\index.js:335:12) at next (D:\temp_project\smpms\node_modules\express\lib\router\index.js:275:10) at D:\temp_project\smpms\node_modules\express\lib\router\index.js:635:15
How can I resolve this problem?
return
where you validate the fields – Daniel_Knightsnull
– Daniel_Knights