Firebase TypeError:undefined不是对象(计算';常量联接';)
如何在状态变量上声明数组 我使用的是react native expo和firebase都是最新的Firebase TypeError:undefined不是对象(计算';常量联接';),firebase,react-native,expo,Firebase,React Native,Expo,如何在状态变量上声明数组 我使用的是react native expo和firebase都是最新的 export default class Profile extends Component { state = { ageRangeValues: this.props.user.ageRange, distanceValue: [this.props.user.distance], } render() { const { ageRangeValues,
export default class Profile extends Component {
state = {
ageRangeValues: this.props.user.ageRange,
distanceValue: [this.props.user.distance],
}
render() {
const {
ageRangeValues,
distanceValue,
} = this.state;
return (
<View>
<Slider
min={5}
max={100}
values={distanceValue}
onValuesChange={val => this.setState({ distanceValue: val })}
onValuesChangeFinish={val => this.updateUser('distance', val[0])}
/>
<Slider
min={18}
max={70}
values={ageRangeValues}
onValuesChange={val => this.setState({ ageRangeValues: val })}
onValuesChangeFinish={val => this.updateUser('ageRange', val)}
/>
</View>) }
导出默认类配置文件扩展组件{
状态={
AgrangeValues:this.props.user.Agrange,
距离值:[this.props.user.distance],
}
render(){
常数{
价值观,
距离值,
}=本州;
返回(
this.setState({distanceValue:val})}
onValuesChangeFinish={val=>this.updateUser('distance',val[0])}
/>
this.setState({ageRangeValues:val})
onvalueschangefish={val=>this.updateUser('ageRange',val)}
/>
) }
我希望这可以正常工作,但AgrangeValue未定义,但defined中的distanceValue不知道为什么可能是因为AgrangeValue接受Agrange及其数组。如果我声明areRangeValue:[19,20],一切都会正常,但如果我保持原样,所有的值都是未定义的
这是我的预载
const firebaseConfig = {
apiKey: 'XXXXXXXXX',
databaseURL: 'XXXXX',
storageBucket: 'XXXXX',
};
firebase.initializeApp(firebaseConfig);
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
width: null,
height: null,
resizeMode: 'contain',
},
});
export default class Preload extends Component {
constructor() {
super();
this.loadApp();
// SVGAnimatedLengthList.loadApp();
}
authenticate = (token) => {
const provider = firebase.auth.FacebookAuthProvider
const credential = provider.credential(token);
return firebase.auth().signInWithCredential(credential);
};
_goHome = (user) => {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Home', params: { user } })],
});
this.props.navigation.dispatch(resetAction);
};
loadApp = async () => {
//firebase.auth().signOut();
firebase.auth().onAuthStateChanged((auth) => {
if (auth) {
this.firebaseRef = firebase.database().ref('users');
this.firebaseRef.child(auth.uid).on('value', (snap) => {
const user = firebase.auth().currentUser;
if (user != null) {
this.firebaseRef.child(auth.uid).off('value');
this._goHome(user);
}
});
} else {
this.setState({ showSpinner: false });
this.props.navigation.navigate('Login');
}
});
}
render() {
return (
<ImageBackground source={require('./images/fondo.png')} style={styles.container}>
<ActivityIndicator />
</ImageBackground>
);
}
}
const firebaseConfig={
apiKey:'XXXXXXXXX',
数据库URL:'XXXXX',
storageBucket:'XXXXX',
};
firebase.initializeApp(firebaseConfig);
const styles=StyleSheet.create({
容器:{
弹性:1,
对齐项目:“居中”,
为内容辩护:“中心”,
宽度:空,
高度:空,
resizeMode:'包含',
},
});
导出默认类预加载扩展组件{
构造函数(){
超级();
这个.loadApp();
//SVGAnimatedLengthList.loadApp();
}
身份验证=(令牌)=>{
const provider=firebase.auth.FacebookAuthProvider
const credential=提供者凭据(令牌);
返回firebase.auth().signInWithCredential(凭证);
};
_goHome=(用户)=>{
const resetAction=StackActions.reset({
索引:0,
操作:[NavigationActions.navigate({routeName:'Home',params:{user}})],
});
这个.props.navigation.dispatch(resetAction);
};
loadApp=async()=>{
//firebase.auth().signOut();
firebase.auth().onAuthStateChanged((auth)=>{
if(auth){
this.firebaseRef=firebase.database().ref(“用户”);
this.firebaseRef.child(auth.uid).on('value',(snap)=>{
const user=firebase.auth().currentUser;
如果(用户!=null){
this.firebaseRef.child(auth.uid).off('value');
这是._goHome(用户);
}
});
}否则{
this.setState({showSpinner:false});
this.props.navigation.navigate('Login');
}
});
}
render(){
返回(
);
}
}
尝试使用构造函数
export default class Profile extends Component {
constructor(){
super();
this.state = {
ageRangeValues: this.props.user.ageRange,
distanceValue: [this.props.user.distance],
};
}
render() {
const { ageRangeValues, distanceValue } = this.state;
return (
<View>
<Slider
min={5}
max={100}
values={distanceValue}
onValuesChange={val => this.setState({ distanceValue: val })}
onValuesChangeFinish={val => this.updateUser('distance', val[0])}
/>
<Slider
min={18}
max={70}
values={ageRangeValues}
onValuesChange={val => this.setState({ ageRangeValues: val })}
onValuesChangeFinish={val => this.updateUser('ageRange', val)}
/>
</View>
);
}
导出默认类配置文件扩展组件{
构造函数(){
超级();
此.state={
AgrangeValues:this.props.user.Agrange,
距离值:[this.props.user.distance],
};
}
render(){
const{ageRangeValues,distanceValue}=this.state;
返回(
this.setState({distanceValue:val})}
onValuesChangeFinish={val=>this.updateUser('distance',val[0])}
/>
this.setState({ageRangeValues:val})
onvalueschangefish={val=>this.updateUser('ageRange',val)}
/>
);
}
尝试使用构造函数
export default class Profile extends Component {
constructor(){
super();
this.state = {
ageRangeValues: this.props.user.ageRange,
distanceValue: [this.props.user.distance],
};
}
render() {
const { ageRangeValues, distanceValue } = this.state;
return (
<View>
<Slider
min={5}
max={100}
values={distanceValue}
onValuesChange={val => this.setState({ distanceValue: val })}
onValuesChangeFinish={val => this.updateUser('distance', val[0])}
/>
<Slider
min={18}
max={70}
values={ageRangeValues}
onValuesChange={val => this.setState({ ageRangeValues: val })}
onValuesChangeFinish={val => this.updateUser('ageRange', val)}
/>
</View>
);
}
导出默认类配置文件扩展组件{
构造函数(){
超级();
此.state={
AgrangeValues:this.props.user.Agrange,
距离值:[this.props.user.distance],
};
}
render(){
const{ageRangeValues,distanceValue}=this.state;
返回(
this.setState({distanceValue:val})}
onValuesChangeFinish={val=>this.updateUser('distance',val[0])}
/>
this.setState({ageRangeValues:val})
onvalueschangefish={val=>this.updateUser('ageRange',val)}
/>
);
}
Vencovsky就在传递数据的预览页面上
loadApp = async () => {
//firebase.auth().signOut();
firebase.auth().onAuthStateChanged((auth) => {
if (auth) {
this.firebaseRef = firebase.database().ref('users');
this.firebaseRef.child(auth.uid).on('value', (snap) => {
const user = firebase.auth().currentUser;
if (user != null) {
this.firebaseRef.child(auth.uid).off('value');
this._goHome(user);
}
});
} else {
this.setState({ showSpinner: false });
this.props.navigation.navigate('Login');
}
});
}
更改const user=firebase.auth().currentUser;to const user=snap.val();
做了这个把戏Vencovsky就在传递数据的预览页面上
loadApp = async () => {
//firebase.auth().signOut();
firebase.auth().onAuthStateChanged((auth) => {
if (auth) {
this.firebaseRef = firebase.database().ref('users');
this.firebaseRef.child(auth.uid).on('value', (snap) => {
const user = firebase.auth().currentUser;
if (user != null) {
this.firebaseRef.child(auth.uid).off('value');
this._goHome(user);
}
});
} else {
this.setState({ showSpinner: false });
this.props.navigation.navigate('Login');
}
});
}
更改const user=firebase.auth().currentUser;to const user=snap.val();
首先,你不能在渲染中返回两个
视图
s,你只需要返回一个同时包含这两个视图
s的视图
;其次,你需要提供这个.props.user
是如何提交的,因为如果AgrangeValue
未定义,这是因为道具的作用距离值起作用而AgrangeValue不起作用。它们来自另一个页面,用户uid通过导航选项传递。当你使用console.logthis.props.user.ageRange时,你会得到什么?它抛出数组[19 34]首先,你不能在渲染中返回两个视图
,你只需要返回一个同时包含这两个视图的视图
,然后,您需要提供this.props.user
是如何提交的,因为如果ageRangeValue
未定义,这是因为从props提交的内容距离值起作用,但ageRangeValue不起作用。它们来自另一个页面,用户uid通过导航选项传递。当你使用console.logthis.props.user.ageRange时,你会得到什么?在添加构造函数后,它会抛出数组[19 34]Thks来帮助你。我得到了以下信息:TypeError:undefined不是一个对象(评估'\u this.props.user')为什么要添加“\ux”在这个词之前?它不应该在那里!在添加构造函数后,我得到了很多帮助:TypeError:undefined不是一个对象(计算'\u this.props.user')为什么要在这个词之前添加'\u''?它不应该在那里!