gpt4 book ai didi

javascript - React Native 数据不会在第一次点击时推送到 firebase

转载 作者:行者123 更新时间:2023-11-30 19:34:46 26 4
gpt4 key购买 nike

我正在尝试将 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")"

如何让它第一时间发送本地时间?

最佳答案

如何简单地将 currentDateTime 传递到您的 updateDatabase 调用中?

所以你会这样调用它:

updateDatabase(this.state.textInputField, currentTimeAndDate);

然后函数本身是:

const updateDatabase = (item, time) => {
db.ref('/Database/...').push({
Amount: item,
time: time
});
};

关于javascript - React Native 数据不会在第一次点击时推送到 firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56057885/

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