简体   繁体   English

如何从React Native中的TextField获取值,由于空值而无法将数据存储到数据库

[英]How to get value from TextField in React Native, Unable to store data to database caused by null value

I've been working on react native and laravel within 2 months, and so far so good. 我一直在2个月内研究本机和laravel,到目前为止,一切都很好。 But I got stuck when I am trying to do post method in react native. 但是当我尝试在react native中执行post方法时,我陷入了困境。 I made a simple form with textfield from react native materials. 我用来自本机材料的textfield制作了一个简单的表格。 I knew that the application is connected already with my back-end, but when I want to store data, it shown an error about the column should not be null even I already inserted values. 我知道应用程序已经与后端连接,但是当我要存储数据时,即使我已经插入值,它也会显示有关该列的错误不应为null的信息。

I already check the onChange and the value itself also the method that I declared to do post method. 我已经检查了onChange和值本身以及声明为post方法的方法。 Here is my constructor, textfield, and the onPress for submit Button 这是我的构造函数,文本字段以及用于提交按钮的onPress

Here is my Constructor: 这是我的构造函数:

constructor(){
        super();
        this.state = {
            Ketua: '',
        Level: '',
        }
    }

Here is the Textfield: 这是文本字段:

                <TextField
                    label = 'Ketua Kegiatan'
                    onChange={(data) => { this.setState({Ketua:data.target.value}) }}
                    value = {this.state.Ketua}
                />

                <TextField
                    label = 'Laboratorium'
                    onChange={(data) => { this.setState({lab:data.target.value}) }}
                    value = {this.state.lab}
                />

                <TextField
                    label = 'Level'
                    onChange={(data) => { this.setState({level:data.target.value}) }}
                    value = {this.state.level}
                />

Here is my Submit Method: 这是我的提交方法:

submit(){
        let url = "http:/IP:Localhost/api/pinjams";
        let data = this.state;
        fetch(url,{
            method:'POST',
            headers:{
                "Content-Type" : "application/json",
                "Accept" : "application/json"
            },
            body: JSON.stringify(data)
        })
        .then((result) => {
            result.json().then((resp) => {
                console.warn("resp", resp)
                alert("Data is Submitted")
            })
        })
    } 

And, here is my Submit Button 而且,这是我的提交按钮

<Button
                    style={{fontSize:20, color:'orange'}}
                    styleDisabled={{color:'grey'}}
                    onPress={()=>{this.submit()}}
                    > {"\n"} Submit
                </Button>

But Here is The ERROR MESSAGE: 但是这是错误消息:

resp, Object {
  "exception": "Illuminate\\Database\\QueryException",
  "file": "C:\\xampp\\htdocs\\coba_api_laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
  "line": 664,
  "message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ketua_kegiatan' cannot be null (SQL: insert into `tb_peminjaman_mhs` (`ketua_kegiatan`, `lab`, `level`) values (?, ?, ?))",

If anyone can help me to solve this error, I'd be so thankful for that :) 如果有人可以帮助我解决此错误,我将非常感谢:)

I figured out how to solve mine, i think it's because in the constructor state we need to arrange the variable row same as in database and also match the variable name in onChange with the columns in database. 我想出了解决方法,我认为这是因为在构造函数状态下,我们需要将变量行与数据库中的行排列在一起,并且还要将onChange中的变量名与数据库中的列进行匹配。 What I mean is try to match the variable that you are going to use with the column name in database. 我的意思是尝试将要使用的变量与数据库中的列名进行匹配。 Thank You So much for helping me out ! 非常感谢您对我的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM