mysql - 在状态的发布请求中发送日期始终返回 NULL
问题描述
我正在尝试发送一个包含时间戳类型的日期的发布请求,我正在使用日期选择器,并且在 onchangevalue 中我将日期存储在 this.state.date 中。无论如何,后端工作正常,因为使用应用程序和邮递员添加了请求。
但问题是我从应用程序发送请求的日期最终得到了这个结果:
那是选择器的日期。所以......我不知道有什么问题。这是我的代码
import React, { Component } from "react";
import {
View,
StyleSheet,
FlatList,
ListView,
ImageBackground,
Dimensions,
Image,
Alert
} from "react-native";
import {
Container,
Header,
Left,
Body,
Right,
Title,
Subtitle,
Icon,
Content,
Footer,
FooterTab,
Button,
Text,
Badge,
List,
ListItem,
Form,
Picker,
DatePicker
} from "native-base";
import Icon0 from "react-native-vector-icons/MaterialCommunityIcons";
import Icon1 from "react-native-vector-icons/FontAwesome";
import CountDown from "react-native-countdown-component";
import moment from "moment";
import "moment/locale/fr";
import bg from "../../assets/images/tryb.png";
import logo from "../../assets/images/trylogo.png";
import Iconbtn from "@expo/vector-icons/Ionicons";
const { width: WIDTH } = Dimensions.get("window");
class Prendre extends Component {
constructor(props) {
super(props);
this.state = {
selected: "key2"
};
this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
};
this.state = { chosenDate: new Date() };
this.setDate = this.setDate.bind(this);
}
setDate(newDate) {
this.setState({ chosenDate: newDate });
}
onValueChange(value: string) {
this.setState({
selected: value
});
}
addRendezVous = () => {
Alert.alert(this.state.date);
var details = {
idmedecin: 1,
idpatient: 1,
etat: 1,
date: this.state.date
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch("http://***.***.***.***:3000/rendezvousinsert", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},
body: formBody
});
};
render() {
return (
<Container style={{ alignItems: "center" }}>
<ImageBackground source={bg} style={styles.Backgroundcontainer}>
<Content>
<Form style={{ alignItems: "center", marginTop: 15 }}>
<Image source={logo} style={styles.logo} />
<Text
style={{
fontFamily: "Ionicons",
fontSize: 20,
fontWeight: "bold",
color: "#0c75b0",
textAlign: "center",
marginTop: 35
}}
>
{" "}
votre rendez vous avec le docteur du spécialité{" "}
</Text>
<Picker
mode="dialog"
iosHeader="Select your SIM"
iosIcon={<Icon name="arrow-down" />}
style={{ width: WIDTH }}
selectedValue={this.state.selected}
onValueChange={this.onValueChange.bind(this)}
itemStyle={{
backgroundColor: "grey",
color: "blue",
fontFamily: "Ionicons",
fontSize: 17
}}
>
<Picker.Item label="Wallet" value="key0" />
<Picker.Item label="ATM Card" value="key1" />
<Picker.Item label="Debit Card" value="key2" />
<Picker.Item label="Credit Card" value="key3" />
<Picker.Item label="Net Banking" value="key4" />
</Picker>
<Text
style={{
fontFamily: "Ionicons",
fontSize: 20,
fontWeight: "bold",
color: "#0c75b0",
textAlign: "center"
}}
>
{" "}
Au nom du{" "}
</Text>
<Picker
mode="dialog"
iosHeader="Select your SIM"
iosIcon={<Icon name="arrow-down" />}
style={{ width: WIDTH }}
selectedValue={this.state.selected}
onValueChange={this.onValueChange.bind(this)}
>
<Picker.Item label="Wallet" value="key0" />
<Picker.Item label="ATM Card" value="key1" />
<Picker.Item label="Debit Card" value="key2" />
<Picker.Item label="Credit Card" value="key3" />
<Picker.Item label="Net Banking" value="key4" />
</Picker>
<Text
style={{
fontFamily: "Ionicons",
fontSize: 20,
fontWeight: "bold",
color: "#0c75b0",
textAlign: "center"
}}
>
{" "}
A la date du
</Text>
<DatePicker
defaultDate={new Date(2018, 4, 4)}
minimumDate={new Date(2018, 1, 1)}
maximumDate={new Date(2018, 12, 31)}
locale={"fr"}
timeZoneOffsetInMinutes={undefined}
modalTransparent={false}
animationType={"fade"}
androidMode={"default"}
placeHolderText="cliquez ici pour choisir une date"
textStyle={{
fontFamily: "Ionicons",
fontWeight: "bold",
color: "#cf1d76",
textAlign: "center"
}}
placeHolderTextStyle={{ color: "#D3D3D3" }}
onDateChange={(this.setDate, date => this.setState({ date }))}
disabled={false}
/>
<Text
style={{
fontFamily: "Ionicons",
fontSize: 15,
fontWeight: "bold",
color: "#cf1d76",
textAlign: "center"
}}
>
Date: {this.state.chosenDate.toString().substr(4, 12)}
</Text>
<Button
onPress={this.addRendezVous.bind(this)}
style={{
justifyContent: "center",
marginHorizontal: "30%",
color: "#0c75b0",
marginTop: 30,
width: 170
}}
>
<Iconbtn
name="md-checkbox"
style={{
fontSize: 24,
marginLeft: 5,
borderRadius: 25,
color: "white"
}}
/>
<Text>envoyer</Text>
</Button>
</Form>
</Content>
</ImageBackground>
</Container>
);
}
}
export default Prendre;
const styles = StyleSheet.create({
container: {
flex: 1
},
Backgroundcontainer: {
flex: 1,
alignItems: "center"
},
logo: {
justifyContent: "center",
width: 128,
height: 155,
marginTop: 5
}
});
找到了一个临时解决方案:我使用 moment 转换了日期并且它有效,但我想知道为什么它首先不起作用解决方案代码`addRendezVous = () => { const date = moment(this.state.date)。格式(“YYYY/MM/DD”);Alert.alert(日期);
var details = {
'idmedecin': 1,
'idpatient': 1,
'etat': 1,
'date': date
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch('http://192.168.1.107:3000/rendezvousinsert', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
})
}`
解决方案
在您的构造函数中,您将两次初始化您的状态。
this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
};
this.state = { chosenDate: new Date() };
当您第二次使用它时,您的 date 初始值将变为 null 而不是空字符串,因为它已被覆盖。这使得 this.state.date = null。你应该做的是
this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
chosenDate: new Date()
};
这将使您的其余变量保持不变。现在,您应该将 this.state.date 视为空字符串,而不是 null。
推荐阅读
- android - 片段中的 Lateinit 属性尚未初始化
- c# - Dotfuscator - 无法解析 System.Reflection.BindingFlags
- python - Keras 中的计划采样
- gremlin - 在 Gremlin 中,如何在一个属性上查询两个或多个具有相同值的顶点?
- c# - 输入字符串的格式不正确。不知道如何转换输入字符串c#
- jhipster - zsh:找不到命令:jhipster Catalina mac
- json - 用于 Angular 的 RestFul API 的 JSON 数据列表的最佳架构
- node.js - 无法返回链式承诺结果
- html - html type="number" : 在点之后总是显示 2 位数字?
- javascript - 是否有任何用于 React 的日期选择器允许自由格式化的自由输入以及日历选择器