最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

来自后端的输入验证显示在控制台上,但不显示在前端React,nodejs引导程序上

网站源码admin18浏览0评论

来自后端的输入验证显示在控制台上,但不显示在前端React,nodejs引导程序上

来自后端的输入验证显示在控制台上,但不显示在前端React,nodejs引导程序上

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);

这些错误显示在“网络”选项卡上,但拒绝显示在每个字段下的前端,我使用了引导程序[[classnames is-valid尝试在显示它时使用了很多选项,用我所见的大部分内容,将近两周都无法使用,根本没有运气。

请任何帮助将不胜感激。在此先感谢

我尝试提交空白字段时显示的错误,该错误显示在“网络”标签中,但未显示在前端

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承诺的使用存在问题。在开发人员控制台上,错误显示:“未捕获(承诺)”。您正在使用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/axios/axios/issues/960#issuecomment-309287911

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

发布评论

评论列表(0)

  1. 暂无评论