【发布时间】:2019-05-09 10:58:05
【问题描述】:
我正在尝试将 TextInput 字段中的数据和创建的时间戳都推送到 Firebase 实时数据库。但是,当应用程序加载时,第一次点击提交只提交文本输入而不是“时间戳”
这是一个简单版本的文件,
import React, { Component } from 'react';
import { View, Text, TouchableHighlight, StyleSheet,
ImageBackground, TextInput, Alert, Picker, TouchableWithoutFeedback,
Keyboard } from 'react-native';
import React, { Component } from 'react';
import { View, Text, TouchableHighlight, StyleSheet,
ImageBackground, TextInput, Alert, Picker, TouchableWithoutFeedback,
Keyboard } from 'react-native';
import { db } from '../config';
let date = new Date().getDate(); //Current Date
let month = new Date().getMonth() + 1; //Current Month
let year = new Date().getFullYear(); //Current Year
let hours = new Date().getHours(); //Current Hours
let min = new Date().getMinutes(); //Current Minutes
let sec = new Date().getSeconds(); //Current Seconds
let currentTimeAndDate
const updateDatabase = item => {
db.ref('/Database/...').push({
Amount: item,
time: currentTimeAndDate
});
};
export default class AddItem extends Component {
constructor(props) {
super (props);
this.state = {
pickerSelection: "default value!",
date: " "
};
}
handleSubmit = () => {
this.setState({
//Setting the value of the date time
date:
date + '/' + month + '/' + year + ' ' + hours + ':' + min +
':' + sec,
});
currentTimeAndDate = this.state.date
if (this.state.pickerSelection === "WHATEVER") {
updateDatabase(this.state.textInputField);
Alert.alert("database updated!");
//This is a short version of the View:
render() {
return (
<View>
<TouchableHighlight
onPress={TextInput.CheckTextInputIsEmptyOrNot}
onPress={this.handleSubmit}
>
<Text style={styles.buttonText}>Update</Text>
</TouchableHighlight>
</View>
);
}
我已经尝试在单击 TouchableHighLight 时在文本中显示 currentTimeAndDate。加载应用程序后,它不会显示第一次点击,但会显示第二次。
在firebase中,第一次点击时数据是这样输入的:
EJDPvodisjpINJI_9
amount: "(input number fx. 10)"
time: " "
第二次点击:
Efvpspodvpoep_10
amount: "(input number fx. "10")"
time: "(the local time of click fx. "8/5/2019 19:28:28")"
我该怎么做才能让它第一次发送当地时间?
【问题讨论】:
标签: javascript firebase react-native firebase-realtime-database state