Я пытаюсь сделать страницы входа и регистрации, страница входа правильно получает все переменные, а регистрация по какой-то причине передает только пароль.
Приложение.JS:
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({extended: true,}));
app.use(bodyParser.json());
const port = process.env.PORT || 5555;
const usersSignup = [];
const { Client } = require("pg");
//Create static files
app.use(express.static(__dirname));
app.post("/sign-in", function (req, res) {
let email = req.body.email;
let pass = req.body.psw;
index = usersSignup.findIndex((x) => x.user === email && x.pass === pass);
if (index != -1) {
if (email == "[email protected]" && pass == "Admin") {
res.redirect("/contact");
}
res.send(
`Welcome!` + JSON.stringify(email) + "Password:" + JSON.stringify(pass)
);
console.info("Hello");
} else {
res.send("User not found");
console.info("User not found");
}
});
app.post("/sign-up", function (req, res) {
console.info(req.body);
var Fname = req.body.firstName;
var Lname = req.body.lastName;
var pass = req.body.Password;
var email = req.body.email;
var pCode = req.body.promoCode;
con.query("select * from users where email=$1", [email], (err, res) => {
var result = JSON.stringify(res.rows[1]);
if (result != null) {
console.info("User exists");
} else {
con.query(
"INSERT INTO users (Name,FamilyName,Email,PromoCode,Password) values($1,$2,$3,$4,$5)",
[Fname, Lname, email, pass, pCode]
);
}
});
Форма регистрации - req.body получает только пароль (psw) и ничего больше... я пытался отключить модальное окно, но та же проблема. связанная функция состоит в том, чтобы проверить, что оба пароля одинаковы, и проверить ограничения пароля (6 цифр, заглавные буквы и т. д.)
<form method = "POST" action = "sign-up">
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "text" class = "form-control form-control-user" id = "firstName"
placeholder = "First Name">
</div>
<div class = "col-sm-6">
<input type = "text" class = "form-control form-control-user" id = "lastName"
placeholder = "Last Name">
</div>
</div>
<div class = "form-group">
<input type = "email" class = "form-control form-control-user" id = "email"
placeholder = "Email Address">
</div>
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "password" onclick = "validFunc()" class = "form-control" id = "Password"
name = "psw" placeholder = "Password" required>
</div>
<div class = "col-sm-6">
<input type = "password" class = "form-control form-control-user" id = "RePassword"
placeholder = "Repeat Password">
</div>
</div>
<div class = "form-group">
<input type = "test" class = "form-control form-control-user" id = "promoCode"
placeholder = "Promo Code">
</div><br>
<button type = "button" onclick = "displayForm()" class = "btn btn-primary" data-toggle = "modal"
data-target = "#ModalMessage">Sign Up</button>
<div class = "signUp"><br>
<hr>
<p><b>Already have an account? <a href = "/sign-in">Sign In</a></b></p>
</div>
<!-- Modal -->
<div class = "modal fade" id = "ModalMessage" tabindex = "-1" role = "dialog"
aria-labelledby = "ModalMessageLabel" aria-hidden = "true">
<div class = "modal-dialog" role = "document">
<div class = "modal-content">
<div class = "modal-header">
<h5 class = "modal-title" id = "ModalMessageLabel">Check details</h5>
<button type = "button" class = "close" data-dismiss = "modal" aria-label = "Close">
<span aria-hidden = "true">×</span>
</button>
</div>
<div class = "modal-body" id = "MBody">
Please review the details and confirm:
<p id = "message"></p>
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-secondary"
data-dismiss = "modal">Cancel</button>
<button type = "submit" class = "btn btn-primary"
onclick = "sendMail()">Confirm</button>
</div>
</div>
</div>
</div>
<!-- End Modal -->
</form>
Форма входа (Отлично работает) - все данные приходят как надо.
<form method = "POST" action = "sign-in">
<div class = "input-group mb-3">
<div class = "input-group-prepend">
<span class = "input-group-text" id = "basic-addon1"><img
src = "/style/outline_account_box_black_18dp.png" alt = ""></span>
</div>
<input type = "email" class = "form-control" id = "email" name = "email"
placeholder = "Enter email" required>
</div>
<div class = "input-group mb-3">
<div class = "input-group-prepend">
<span class = "input-group-text" id = "basic-addon1"><img
src = "/style/outline_lock_black_18dp.png" alt = ""></span>
</div>
<input type = "password" onclick = "validFunc()" class = "form-control" id = "Password"
name = "psw" placeholder = "Password" title = " Must contain at least one number and one uppercase and lowercase letter and one special
char, and at least 6 or more characters" required>
</div>
<div class = "g-recaptcha" data-sitekey = "6LeryQAaAAAAACtRGmnGbDekQjszQXzXrH858eJe"></div><br>
<button type = "submit" class = "btn btn-primary" onclick = "return alerts()">Log In</button>
<div class = "sign-up"><br>
<hr>
<p><b>Need an account? <a href = "/sign-up">Sign Up</a></b></p>
</div>
</form>
Вы не указали атрибут имени для регистрации!
<form method = "POST" action = "sign-up">
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "text" class = "form-control form-control-user" id = "firstName" name = "firstName"
placeholder = "First Name">
</div>
<div class = "col-sm-6">
<input type = "text" class = "form-control form-control-user" id = "lastName" name = "lastName"
placeholder = "Last Name">
</div>
</div>
<div class = "form-group">
<input type = "email" class = "form-control form-control-user" id = "email" name = "email"
placeholder = "Email Address">
</div>
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "password" onclick = "validFunc()" class = "form-control" id = "Password"
name = "psw" placeholder = "Password" required>
</div>
<div class = "col-sm-6">
<input type = "password" class = "form-control form-control-user" id = "RePassword" name = "RePassword"
placeholder = "Repeat Password">
</div>
</div>
<div class = "form-group">
<input type = "test" class = "form-control form-control-user" id = "promoCode" name = "promoCode"
placeholder = "Promo Code">
</div><br>
<button type = "button" onclick = "displayForm()" class = "btn btn-primary" data-toggle = "modal"
data-target = "#ModalMessage">Sign Up</button>
<div class = "signUp"><br>
<hr>
<p><b>Already have an account? <a href = "/sign-in">Sign In</a></b></p>
</div>
<!-- Modal -->
<div class = "modal fade" id = "ModalMessage" tabindex = "-1" role = "dialog"
aria-labelledby = "ModalMessageLabel" aria-hidden = "true">
<div class = "modal-dialog" role = "document">
<div class = "modal-content">
<div class = "modal-header">
<h5 class = "modal-title" id = "ModalMessageLabel">Check details</h5>
<button type = "button" class = "close" data-dismiss = "modal" aria-label = "Close">
<span aria-hidden = "true">×</span>
</button>
</div>
<div class = "modal-body" id = "MBody">
Please review the details and confirm:
<p id = "message"></p>
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-secondary"
data-dismiss = "modal">Cancel</button>
<button type = "submit" class = "btn btn-primary"
onclick = "sendMail()">Confirm</button>
</div>
</div>
</div>
</div>
<!-- End Modal -->
</form>
Вы забыли name
атрибуты
<input type = "text" class = "form-control form-control-user" id = "firstName"
placeholder = "First Name">
Должно быть
<input type = "text" class = "form-control form-control-user" id = "firstName" name = "firstName"
placeholder = "First Name">
HTML-формы используют name
для отправки данных, а не id
Сделайте это для всех входных данных, также вы написали name = "psw"
на HTML, но делаете req.body.Password
на серверной части, вам нужно исправить и это.
Фиксированный HTML:
<form method = "POST" action = "sign-up">
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "text" class = "form-control form-control-user" id = "firstName" name = "firstName"
placeholder = "First Name">
</div>
<div class = "col-sm-6">
<input type = "text" class = "form-control form-control-user" id = "lastName" name = "lastName"
placeholder = "Last Name">
</div>
</div>
<div class = "form-group">
<input type = "email" class = "form-control form-control-user" id = "email" name = "email"
placeholder = "Email Address">
</div>
<div class = "form-group row">
<div class = "col-sm-6 mb-3 mb-sm-0">
<input type = "password" onclick = "validFunc()" class = "form-control" id = "Password" name = "Password"
placeholder = "Password" required>
</div>
<div class = "col-sm-6">
<input type = "password" class = "form-control form-control-user" id = "RePassword"
placeholder = "Repeat Password">
</div>
</div>
<div class = "form-group">
<input type = "test" class = "form-control form-control-user" id = "promoCode" name = "promoCode"
placeholder = "Promo Code">
</div><br>
<button type = "button" onclick = "displayForm()" class = "btn btn-primary" data-toggle = "modal"
data-target = "#ModalMessage">Sign Up</button>
<div class = "signUp"><br>
<hr>
<p><b>Already have an account? <a href = "/sign-in">Sign In</a></b></p>
</div>
<!-- Modal -->
<div class = "modal fade" id = "ModalMessage" tabindex = "-1" role = "dialog"
aria-labelledby = "ModalMessageLabel" aria-hidden = "true">
<div class = "modal-dialog" role = "document">
<div class = "modal-content">
<div class = "modal-header">
<h5 class = "modal-title" id = "ModalMessageLabel">Check details</h5>
<button type = "button" class = "close" data-dismiss = "modal" aria-label = "Close">
<span aria-hidden = "true">×</span>
</button>
</div>
<div class = "modal-body" id = "MBody">
Please review the details and confirm:
<p id = "message"></p>
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-secondary"
data-dismiss = "modal">Cancel</button>
<button type = "submit" class = "btn btn-primary"
onclick = "sendMail()">Confirm</button>
</div>
</div>
</div>
</div>
<!-- End Modal -->
</form>
Можете ли вы опубликовать содержимое validFunc()
, которое вы использовали для onClick
пароля, а также содержимое displayForm()
, используемое при отправке формы? Я подозреваю, что недостающие поля каким-то образом попали undefined
с вашим кодом проверки контента.
Спасибо!! не могу поверить, что я этого не заметил (массивный фейспалм)