Javascript (firebase错误)设置失败第一个参数在属性中包含未定义
单击“添加”按钮时会出现此错误,但它仍会将输入文本添加到firebase中 我如何消除这个错误Javascript (firebase错误)设置失败第一个参数在属性中包含未定义,javascript,firebase,react-native,firebase-realtime-database,Javascript,Firebase,React Native,Firebase Realtime Database,单击“添加”按钮时会出现此错误,但它仍会将输入文本添加到firebase中 我如何消除这个错误 import React, { Component } from 'react'; import { View, Text, StyleSheet, Button, TextInput, TouchableOpacity, ScrollView } from 'react-native'; import firebase from './firebase'; export d
import React, { Component } from 'react';
import { View, Text, StyleSheet, Button, TextInput, TouchableOpacity, ScrollView } from 'react-native';
import firebase from './firebase';
export default class App extends Component {
carDatabase = firebase.database().ref('car');
state = { cars: {}, selectedId: '', text: ''}
// Read
componentDidMount() {
this.carDatabase.on('value', cars => {
const carsJSON = cars.val();
this.setState({ cars: carsJSON === null ? {} : carsJSON });
})
// this.carDatabase.push({color: 'yellow'})
}
// Create
create() {
this.carDatabase.push('space') /// changes selected item
this.setState({selectedId: ''})
}
add() {
this.carDatabase.push(this.state.text).set(TextInput.value) /// pushes textput to firebase
this.setState(TextInput.value)
}
// Update
update() {
this.carDatabase.child(this.state.selectedId).set('space') /// changes text of selected item
this.setState({selectedId: ''})
}
// Delete
deleteCar() {
if(this.state.selectedId === '') { ///selects item
return;
}
this.carDatabase.child(this.state.selectedId).set(null) ///deletes item
this.setState({selectedId: ''})
}
render() {
return (
<View style={styles.container}>
<ScrollView>
<Text style={styles.top}>Todo List</Text>
<TextInput value={this.state.text} style={styles.textInput}></TextInput>
<Button title="create" onPress={() => this.create()}></Button>
<Button title="update" onPress={() => this.update()}></Button>
<Button title="delete" onPress={() => this.deleteCar()}></Button>
<Button title="add" onPress={() => this.add()}></Button>
{
Object.keys(this.state.cars).map( (carId, index, text) =>
<TouchableOpacity key={index} onPress={() => this.setState({ selectedId: carId, text})}>
<Text style={styles.input}>{`${JSON.stringify(this.state.cars[carId])}`} </Text>
</TouchableOpacity>
)
}
<TextInput ///creates text input
style={{height: 40}}
placeholder="Type here to add"
onChangeText={(text) => this.setState({text})}
value={this.state.text} ///gets text input
/>
<Text style={{padding: 10, fontSize: 42}}> {this.state.text.value} </Text>
</ScrollView>
{/* <Text>{JSON.stringify(this.state.cars, null, 2)}</Text> */}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
width: '100%'
},
textInput: {
backgroundColor: '#ADD8E6',
height: 40,
width: '100%'
},
input: {
backgroundColor: '#ADD8E6',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
},
top: {
backgroundColor: 'white',
height: 45,
paddingLeft: 140,
paddingTop: 20,
fontSize: 20,
alignItems: 'center',
justifyContent: 'center'
}
});
import React,{Component}来自'React';
从“react native”导入{View,Text,StyleSheet,Button,TextInput,TouchableOpacity,ScrollView};
从“/firebase”导入firebase;
导出默认类应用程序扩展组件{
carDatabase=firebase.database().ref('car');
state={cars:{},selectedId:'',text:'}
//阅读
componentDidMount(){
此.carDatabase.on('value',cars=>{
const carsJSON=cars.val();
this.setState({cars:carsJSON==null?{}:carsJSON});
})
//this.carDatabase.push({color:'yellow'})
}
//创造
创建(){
this.carDatabase.push('space')///更改所选项目
this.setState({selectedId:''})
}
添加(){
this.carDatabase.push(this.state.text).set(TextInput.value)///将textput推送到firebase
this.setState(TextInput.value)
}
//更新
更新(){
this.carDatabase.child(this.state.selectedId.set('space')///更改所选项目的文本
this.setState({selectedId:''})
}
//删除
deleteCar(){
如果(this.state.selectedId==''){///选择项
返回;
}
this.carDatabase.child(this.state.selectedId.set(null)///删除项
this.setState({selectedId:''})
}
render(){
返回(
事項清單
this.create()}>
this.update()}>
this.deleteCar()}>
this.add()}>
{
Object.keys(this.state.cars).map((carId,index,text)=>
this.setState({selectedId:carId,text}}>
{`${JSON.stringify(this.state.cars[carId])}`}
)
}
this.setState({text})}
value={this.state.text}///获取文本输入
/>
{this.state.text.value}
{/*{JSON.stringify(this.state.cars,null,2)}*/}
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
背景颜色:“#fff”,
宽度:“100%”
},
文本输入:{
背景颜色:“#添加8e6”,
身高:40,
宽度:“100%”
},
输入:{
背景颜色:“#添加8e6”,
填充:20,
Margin:8,
marginHorizontal:16,
},
顶部:{
背景颜色:“白色”,
身高:45,
paddingLeft:140,
paddingTop:20,
尺寸:20,
对齐项目:“居中”,
为内容辩护:“中心”
}
});
从您描述的方式来看,这是导致问题的呼叫:
this.carDatabase.push(this.state.text).set(TextInput.value)
既然您说数据库仍在更新,那么推送(…)似乎可以工作,但是设置(…)失败了。正如错误消息所说,这可能是由于TextInput.value
未定义引起的
如果确实如此,请在将TextInput.value
写入数据库之前记录其值,并验证其是否符合预期
如果以上不是原因,那么很难确定问题的确切来源是什么。我建议您缩小问题和代码的范围。阅读以了解为什么以及如何做到这一点,这将极大地增加有人可能提供帮助的机会,并通常会减少你的问题所吸引的反对票