gpt4 book ai didi

node.js - 从控制台上显示的后端进行输入验证,但在前端React上不显示,nodejs Bootstrap

转载 作者:行者123 更新时间:2023-12-03 08:40:43 25 4
gpt4 key购买 nike

ContactUs.js组件

import React, { Component } from "react";
import { Button, Row, Col, Form } from "react-bootstrap";
import axios from "axios";
import { MDBContainer, MDBRow, MDBCol, MDBInput } from "mdbreact";
import { withRouter } from "react-router-dom";

class Contact extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
firstName: "",
lastName: "",
subject: "",
message: "",
errors: {},
};

this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}

onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}

onSubmit(e) {
e.preventDefault();
try {
const contactUs = {
email: this.state.email,
firstName: this.state.firstName,
lastName: this.state.lastName,
subject: this.state.subject,
message: this.state.message,
};

axios
.post("http://localhost:12345/api/contactUs", contactUs)
.then((res) => this.props.history.push("/"));
} catch (errors) {
this.setState({
errors: errors.response.data,
});
}
}

render() {
const { errors } = this.state;
return (
<section id="contact" className="contact-area pt-120 pb-120" style={{ width: "105"}}>
<div className="container">
<div className="row">
<MDBContainer>
<MDBRow>
<MDBCol >
<form onSubmit={this.onSubmit}>
<h3 className="sub-title" style={{ textAlign: "center" }}>
We Would Like To Hear From You
</h3>
<div className="grey-text">
<MDBInput
label="Your email"
icon="envelope"
group
type="email"
validate
error="wrong"
success="right"
name="email"
value={this.state.email}
onChange={this.onChange}
/>

<MDBInput
label="First Name"
icon="user"
group
type="text"
validate
error="wrong"
success="right"
name="firstName"
value={this.state.firstName}
onChange={this.onChange}
/>

<MDBInput
label="Last Name"
icon="user"
group
type="text"
validate
error="wrong"
success="right"
name="lastName"
value={this.state.lastName}
onChange={this.onChange}
/>

<MDBInput
label="Subject"
icon="tag"
group
required
type="text"
validate
error="wrong"
success="right"
name="subject"
value={this.state.subject}
onChange={this.onChange}
/>

<MDBInput
type="textarea"
rows="8"
label="Your message"
icon="pencil-alt"
name="message"
value={this.state.message}
onChange={this.onChange}
/>
</div>
<div className="text-center">
<Row>
<Col>
<Form.Group controlId="submit" outline="{true}" far="{true}" icon="paper-plane" className="ml-1">
<Button
// className="main-btn"
type="submit"
variant="outline-primary"
>
Send
</Button>
</Form.Group>
</Col>
</Row>
</div>
</form>
</MDBCol>
</MDBRow>
</MDBContainer>
</div>
</div>
</section>
);
}
}

export default withRouter(Contact);


enter image description here

Those errors are showing on the network tab, but have refused to show on the front-end under each field, i have used bootstrap classnames is-valid to try to use in displaying it, i have goggled alot and use most of what i saw, it just wouldn't work, no luck at all, for almost two weeks now.

Please any help will be greatly appreciated. thanks in advance



当我尝试提交空白字段时显示的错误,该错误显示在“网络”标签中,但未显示在前端
enter image description here
contact.us.form.js (backend route)

const express = require("express");
const { check } = require("express-validator");

const ContactUsForm = require("../controllers/contact-us-form");
const validate = require("../middlewares/validate");

const router = express.Router();


router.post('/contactUs', [
check('email').isEmail().withMessage('Enter a valid Work email address'),
check('firstName').not().isEmpty().withMessage('Please your first name is required'),
check('lastName').not().isEmpty().withMessage('Your last name is also required'),
check('subject').not().isEmpty().withMessage('Please you might want us to know why you are contacting us'),
check('message').not().isEmpty().withMessage('Please Leave us a message'),
],
validate,
ContactUsForm.contactUs);

module.exports = router;

`

最佳答案

我认为您对axios promise 的使用存在问题。在开发人员控制台上,错误显示:“未捕获( promise )”。您正在使用try-catch,但也许也可以尝试使用axios的catch方法,例如:

axios
.post("http://localhost:12345/api/contactUs", contactUs)
.then(res => this.props.history.push("/"))
.catch(
error => { console.log(error.response) }
);

我相信这个答案可能会有所帮助: https://github.com/axios/axios/issues/960#issuecomment-309287911

希望这会有所帮助。祝好运!

关于node.js - 从控制台上显示的后端进行输入验证,但在前端React上不显示,nodejs Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61797977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com