React Native-将数据添加到数据库不起作用

时间:2018-07-24 21:00:13

标签: javascript android database reactjs react-native

我想知道什么是正确的方法:目前,我有一个来自数据库的“ item / s”用于显示,从这里我正尝试将“ item / s”发送回我的数据库用户点击“发送”按钮后。

使用此代码,我得到以下输出:

Nodemon示例:

Example_1 (Nodemon)

console.log示例:

Example_2 (Console.log)

这是我的代码

export default class Dishes extends Component {
    constructor(props) {
        super (props)
        this.state = {
            data: [],
            id: [],
            price: [],
            count: '',
            SplOrder: '',
            name: '',
            menu_price: '',
            tbl: this.props.navigation.state.params.tbl,
            orderDet: this.props.navigation.state.params.orderDet,
        }
    }

submit = ({ item, index }) => {
            fetch('http://192.168.254.102:3308/SendOrder/Send', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    order_no: this.state.orderDet,
                    tbl_id: this.state.tbl,
                    //special_request: this.state.SplOrder,
                    menu_desc: this.state.name,              //item
                    menu_price: this.state.menu_price,       //item
                    //order_quantity: this.state.count,
                })
            }).then(res => res.json())
                .then((responseJson) => {
                    Alert.alert(JSON.stringify(responseJson))
                    console.log(responseJson);
                })
            .catch(error => console.error('Error:', error))
}
componentDidMount() {
    this.fetchData();
}

_renderItem = ({ item, index }) => {
    return (
                <View>
                    <Text>Name: { name }</Text>
                    <Text>Price: ₱{ price }</Text>
                    <Text>Status: { item.menu_status }</Text>
                </View>
    )
}
render() {
    return (
        <View>

            <FlatList
                data = {this.state.data}
                keyExtractor = {(item, index) => index.toString()}
                extraData = {this.state}
                renderItem = {this._renderItem} />

            <View>
                <TouchableOpacity
                onPress = {(item) => this.submit(item)}>
                    <Text style = {styles.SendText}>Send Order</Text>
                </TouchableOpacity>

            </View>

        </View>
    )
}
}

UI示例:

ScreenShot_sample (UI)

0 个答案:

没有答案