我如何使用React Native插入数据

时间:2018-07-21 12:23:15

标签: node.js reactjs react-native

我想使用nodejs API将数据插入SQL Server数据库中,但是无法从react-native发送数据。我尝试使用axios.post和提取方法无法将数据发送到Web API,但我做不到。我究竟做错了什么?你能帮我吗?

这是我的NODE JS WEB API:

app.post("/insert", function(req, res) {          
    (async function insert(ProName,ProDesc,OnDate) {    
    try {                   
          let pool = await sql.connect(sqlConfig);
          const request= pool.request();   
          request.input("ProName", sql.VarChar, ProName)
          request.input("ProDesc", sql.VarChar, ProDesc)
          request.input("OnDate", sql.VarChar, OnDate)   
          request.query('INSERT INTO Producttbl 
    (ProName,ProDesc,OnDate) VALUES(@ProName,@ProDesc,@OnDate)',     

反应本机代码:

          state = {ProName:'',ProDesc:'',OnDate:''};        
          insert= () =>{
        const {ProName,ProDesc,OnDate}=this.state;
        axios({
          method: 'post',
          url: 'url/insert',
          data: {
           ProName,
            ProDesc,
            OnDate
      },      
      }).then(response =>        

      this.setState({
        ProName:response.ProName,
        ProDesc:response.ProDesc,
        OnDate:response.OnDate
      });
      })

 <TextInput  onChangeText={(ProName)=>this.setState({ProName})} />           

JSON:

     https://i.stack.imgur.com/zEwn2.png

0 个答案:

没有答案