gpt4 book ai didi

mysql - 在来自状态的发布请求中发送日期始终返回 NULL

转载 作者:行者123 更新时间:2023-11-29 16:06:45 24 4
gpt4 key购买 nike

我正在尝试发送一个包含时间戳类型日期的发布请求,我正在使用日期选择器,并在 onchangevalue 中将日期存储在 this.state.date 中。无论如何,后端工作正常,因为使用应用程序和 postman 添加了请求。

但问题是,当我从应用程序发送请求的日期时,我最终得到的结果是:

this

奇怪的部分是日期存储正确,因为当我尝试在警报中显示它时: enter image description here

这是选择器的日期。所以...我不知道出了什么问题。这是我的代码

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).format("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() };

当您第二次使用此值时,日期的初始值将变为 null 而不是空字符串,因为它已被覆盖。这使得 this.state.date = null。你应该做的是

this.state = {
idmedecin: "2",
idpatient: "1",
etat: "1",
date: ""
chosenDate: new Date()
};

这将使其余变量保持不变。现在,您应该将 this.state.date 视为空字符串,而不是 null。

关于mysql - 在来自状态的发布请求中发送日期始终返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com