基本MERN堆栈应用中的Axios发布请求出现问题

时间:2019-12-12 04:12:34

标签: javascript reactjs post axios mern

我想创建一个axios发布请求,该请求将用户选择的公牛和小母牛的ID发送到我的服务器,以计算其后代的特征(在本例中为牛奶生产)。我试图在单击“提交”按钮后触发它。我认为我没有将ID的格式正确的参数发送给服务器进行处理。

import React, { Component} from 'react';
import axios from 'axios';  


class Dropdown extends Component {
    constructor (props) {
      super(props) 


      this.handleInputChange = this.handleInputChange.bind(this); 
      this.onSubmit = this.onSubmit.bind(this);

      this.state = {
        bullId: '',
        heiferId: ''
      }

     }

     //this updates state with new bull & heifer
      handleInputChange(event) {
        const target = event.target;
        const value = target.value;
        target.clicked = target.value;
        const name = target.name;
        console.log(target.name)
;
        this.setState({
          [name]: value
        });
        console.log(this.state);
      }


   handleChange = (event) => {
    var bull = event.target.value
    var heifer = event.target.value
    console.log(heifer)
    console.log(bull)
    };


 onSubmit(e) {
   e.preventDefault();

   const pairing = {
     heifer: this.state.heiferId,
     bull: this.state.bullId
   }
   console.log(pairing)

   axios.post('http://localhost:8000/traits/:bullId/:heiferId', pairing)
    .then(res => console.log(res.data));

    this.setState({
      bullId:"",
      heiferId:""
    })

 }

render() {
    return (
    <div>
      <form>
        <label>Bulls
          <select
          name={"bullId"}
          value ={this.state.bullId} 
          onChange= {this.handleInputChange}> 

            <option value="5defc2b5b9283d6de054e0f0">Buddy</option>
            <option value="5defc2b5b9283d6de054e0f1">Cooper</option>
            <option value="5defc2b5b9283d6de054e0f2">Maxwell</option>
            <option value="5defc2b5b9283d6de054e0f3">Gus</option>
            <option value="5defc2b5b9283d6de054e0f4">Paul</option>
            <option value="5defc2b5b9283d6de054e0f5">Phil</option>
            </select>
          </label>

        <br />


        <label>Heifers
          <select
            name={"heiferId"}
            value={this.state.heiferId}
            onChange= {this.handleInputChange}>

            <option value="5defc49cb9283d6de054e0f6">Sally</option>
            <option value="5defc49cb9283d6de054e0f7">Patches</option>
            <option value="5defc49cb9283d6de054e0f8">Maxine</option>
            <option value="5defc49cb9283d6de054e0f9">Peach</option>
            <option value="5defc49cb9283d6de054e0fa">Paula</option>
            <option value="5defc49cb9283d6de054e0fb">Flower</option>
          </select>
          </label>
       </form>
      <button onClick={this.onSubmit}>submit</button>
    </div>
    )}
}



export default Dropdown;
  Heifer.findOne({_id: req.params.heiferId}).then(function(heifer){
    Bull.findOne({_id: req.params.bullId}).then(function(bull){
      console.log(bull);
      console.log(heifer);

      let heiferMilkProduction = heifer.milkProduction;
      let bullMilkProduction = bull.milkProduction;

      if (heiferMilkProduction > bullMilkProduction) {
        heiferMilkProduction += heiferMilkProduction * .1  
        bullMilkProduction -= bullMilkProduction * .1

      } else {
        bullMilkProduction += bullMilkProduction * .1 
        heiferMilkProduction -= heiferMilkProduction * .1
      };

      const calfTraits = {
        bullMilkProduction,
        heiferMilkProduction
      }

      res.send(calfTraits);
    })
  })
});```

2 个答案:

答案 0 :(得分:0)

您想要类似

 axios.post(`http://localhost:8000/traits/${this.state.bullId}/${this.state.heiferId}`)

字符串中的:bullId语法没有任何反应,您必须像构建其他任何常规字符串一样构建该字符串。它以快递形式用作模板。

答案 1 :(得分:0)

您需要在获取数据而不是字符串的网址中嵌入“ bullId”和“ heiferId”的值。

onSubmit(e) {
e.preventDefault();

const { bullId, heiferId } = this.state;

axios.post(`http://localhost:8000/traits/${bullId}/${heiferId}`, {})
.then(res => console.log(res.data));

this.setState({
  bullId:"",
  heiferId:""
})

}