将数据从React表单输入发送到SQL数据库

时间:2019-04-25 19:25:28

标签: sql reactjs amazon-web-services

嘿,我有一个带有表单输入的react应用程序。每次输入时,我都试图将数据发送到aws aurora sql数据库。由于某种原因,数据没有被发送

表单看起来像这样(这只是其中的2个。.ref相应地匹配)->

<Form.Field>
            <AutoSuggest
              placeholder="eg. Ethereum"
              data={this.props.info.currency}
              labelname="Your Currency Name"
              name="name"
              ref="job"
              getNewValue={this.getNewValue}
              required="true"
            />
          </Form.Field>

          <Form.Field>
            <label style={{ color: "#FFFFFF", fontSize: "24px" }}>
              WhitePaper Upload
              <Icon name="asterisk" color="red" size="tiny" />
            </label>
            <Input
              icon="upload"
              type="file"
              name="link"
              ref="job"
              transparent
              style={{ borderBottom: "1px solid white", paddingBottom: "5px" }}
            />
          </Form.Field>


我的handleSubmit函数看起来像这样->

handleSubmit = e => {
    e.preventDefault();
    const field = e.target;
    // console.log(this.state);
    this.setState(
      {
         ticker: field.ticker.value,
        currency: field.currency.value,
        file: field.file.value,
        launch_date: field.date.value,
        consensus: field.consensus.value,
        support: field.support.value,
        fork: field.fork.value,
        location: field.location.value,
        summary: field.summary.value
      },
      () => {
        this.validate();
      }
      )
    var self = this;
  //  On submit of the form, send a POST request with the data to the server.
    fetch('/info', { 
        method: 'POST',
        data: {
          tick: self.refs.tick,
          name: self.refs.name,
          link: self.refs.link,
          summary: self.refs.summary,
          algo: self.refs.algo,
          mech: self.refs.mech,
          plat: self.refs.plat,
          date: self.refs.date,
          from: self.refs.form,
          location: self.refs.location,
          mineable: self.refs.mineable,
          opensource: self.refs.opensource,
        }
      })
      .then(function(response) {
        return response.json()
      }).then(function(body) {
        console.log(body);
      });
  };

快递服务器上的代码如下->

// POST: add currency view
router.post('/info', jwtAuth, async (req, res) => {
    const data = extractReqData(req);
   await getColumns('CURRENCY_VIEW').then((columns) => {
        const formattedData = Object.assign.apply({}, columns.map((val, idx) => ({ [val]: data[idx] })));
         Aurora.query(sql.contacts.insert, formattedData, (error, results, fields) => {
            error ? res.status(500).json({ err: error }) : res.status(200).json({ response: results });
        });
    });
});

但是,提交表单时,数据根本不会存储在数据库中,而直接进入下一页?

0 个答案:

没有答案