sql - React + SQL Sever 登录表单逻辑
问题描述
嗨,我一直在开发自己的全栈登录。
我创建了一个类来存储用户,现在我正在创建一个从这些存储的用户登录。
我创建了一个基本的登录表单,其中包含对我的 server.js 文件的获取。
但是,当我尝试使用此表单登录时,似乎什么也没发生。
我觉得它背后的逻辑是有道理的,但我似乎无法弄清楚为什么这似乎不起作用。
这是我的班级添加用户
class AddUsers extends React.Component {
constructor() {
super();
this.state = { users: [], email: "", password: "", passwordConfirm: "" };
this.onSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
if (this.state.email.length < 8 || this.state.password.length < 8) {
alert(`please enter the form correctly `);
} else {
const data = { email: this.state.email, password: this.state.password };
fetch("/login-user", {
method: "POST", // or 'PUT'
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log("Success:", data);
})
.catch(error => {
console.error("Error:", error);
});
alert(`Congradulations you have signed up`);
}
}
catch(e) {
console.log(e);
}
render() {
console.log(this.state.users);
return (
<div>
<Formik
class="form-signin"
action="auth"
method="POST"
initialValues={{ email: "", password: "", passwordConfirm: "" }}
onSubmit={(values, { setSubmitting }) => {
setTimeout(() => {
console.log("Logging in", values);
setSubmitting(false);
}, 500);
}}
validationSchema={Yup.object().shape({
email: Yup.string()
.email()
.required("Required")
.matches(
/(?=.*outlook)/,
"This is not a outlook email address."
),
password: Yup.string()
.required("No password provided.")
.min(8, "Password is too short - should be 8 chars minimum.")
.matches(/(?=.*[0-9])/, "Password must contain a number.")
})}
>
{props => {
const {
values,
touched,
errors,
isSubmitting,
handleChange,
handleBlur,
handleSubmit
} = props;
return (
<form
onSubmit={handleSubmit}
class="form-signin"
action="auth"
method="POST"
>
<div className="jumbotron">
<h2>Sign Up </h2>
<div className="help">
<Popup trigger={<Link> Help?</Link>} className="center">
<div>
Enter Codestone Email address and Password connected to
the account.
</div>
</Popup>
</div>
<label htmlFor="email">Email</label>
<input
name="email"
type="email"
placeholder="Enter your email"
value1={values.email}
value={this.state.email}
onInput={handleChange}
onChange={e => this.setState({ email: e.target.value })}
onBlur={handleBlur}
className={errors.email && touched.email && "error"}
/>
{errors.email && touched.email && (
<div className="input-feedback">{errors.email}</div>
)}
<label htmlFor="email">Password</label>
<input
name="password"
type="password"
placeholder="Enter your password"
value2={values.password}
value={this.state.password}
onInput={handleChange}
onChange={e => this.setState({ password: e.target.value })}
onBlur={handleBlur}
className={errors.password && touched.password && "error"}
/>
{errors.password && touched.password && (
<div className="input-feedback">{errors.password} </div>
)}
<button class="button" type="submit" onClick={this.onSubmit}>
Sign Up
</button>
<p>
<Link to="/login"> Login Page </Link>
</p>
<p>
<Link to="/reset"> Reset Password </Link>
</p>
</div>
</form>
);
}}
</Formik>
</div>
);
}
}
这是我的 server.js 路线
根据 chrome 的错误是第一行
app.post("/login-user", function(request, response) {
var req = new sql.Request();
var email = body.email;
var password = body.password;
try {
if (email && password) {
req.query(
"SELECT * FROM TestLogin WHERE email = ? AND password = ?",
[username, password],
function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect("/home");
} else {
response.send("Incorrect Username and/or Password!");
}
response.end();
}
);
} else {
response.send("Please enter Username and Password!");
response.end();
}
} catch (e) {
console.log(e);
response.status(400);
response.send(e);
}
});
添加了镀铬错误
LoginPage.js:42 POST http://localhost:3000/login-user 500 (Internal Server Error)
handleSubmit @ LoginPage.js:42
callCallback @ react-dom.development.js:336
invokeGuardedCallbackDev @ react-dom.development.js:385
invokeGuardedCallback @ react-dom.development.js:440
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:454
executeDispatch @ react-dom.development.js:584
executeDispatchesInOrder @ react-dom.development.js:609
executeDispatchesAndRelease @ react-dom.development.js:713
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:722
forEachAccumulated @ react-dom.development.js:694
runEventsInBatch @ react-dom.development.js:739
runExtractedPluginEventsInBatch @ react-dom.development.js:880
handleTopLevel @ react-dom.development.js:5803
batchedEventUpdates$1 @ react-dom.development.js:24401
batchedEventUpdates @ react-dom.development.js:1415
dispatchEventForPluginEventSystem @ react-dom.development.js:5894
attemptToDispatchEvent @ react-dom.development.js:6010
dispatchEvent @ react-dom.development.js:5914
unstable_runWithPriority @ scheduler.development.js:697
runWithPriority$2 @ react-dom.development.js:12149
discreteUpdates$1 @ react-dom.development.js:24417
discreteUpdates @ react-dom.development.js:1438
dispatchDiscreteEvent @ react-dom.development.js:5881
我已经在 chrome 中测试了 sql 语句,并且在硬编码值时似乎正在工作
解决方案
推荐阅读
- c# - 从gridview内部的datatemplate访问控制
- cygwin - 在 cygwin32 中使用 mingw32 glfw3 和 glew32s 二进制文件,而不将 cygwin X11 GLX 与 WGL 混合
- azure-cloud-services - 是否有每隔一段时间重新启动一个 Azure 经典云服务角色?
- javascript - SweetAlert 不等待
- java - 当我在其 ViewPort 中绘制 JPanel 时,JScrollPane 不滚动
- python - 将文本写入单行字符串之间
- javascript - 将字符串解释为转义字符串
- spring-integration - 基于当前日期的spring集成动态过滤器
- ios - 为什么 webkit 视图无法连接到 App Store
- android - 在“expo publish”之后,React Native Expo 应用程序不会在其他设备上打开