Reactjs React native picker selected值未定义不是对象错误
我正在使用下面的代码作为选择器,并将从选择器中选择的值发送到api调用,我不断收到此错误可能未经处理的承诺拒绝(id:1):undefined不是对象(正在评估'this.state.selected1') 我做错了什么Reactjs React native picker selected值未定义不是对象错误,reactjs,react-native,react-native-android,react-android,Reactjs,React Native,React Native Android,React Android,我正在使用下面的代码作为选择器,并将从选择器中选择的值发送到api调用,我不断收到此错误可能未经处理的承诺拒绝(id:1):undefined不是对象(正在评估'this.state.selected1') 我做错了什么 'use strict' import React, { Component } from 'react'; const ReactNative = require('react-native'); const { Picker, AppRegistry, StyleSheet
'use strict'
import React, { Component } from 'react';
const ReactNative = require('react-native');
const {
Picker,
AppRegistry,
StyleSheet,
Navigator,
Text,
View,
TextInput,
Button,
ScrollView
} = ReactNative;
const Item = Picker.Item;
import { Actions } from 'react-native-router-flux';
import api from './utilities/api';
export default class MarkAttTab extends Component{
constructor(props) {
super(props);
this.state = {
remarkstext: 'Remarks' ,
buttonvalue: 'Submit' ,
selected1: '--Select--',
selected2: '--Select--',
color: 'red',
mode: Picker.MODE_DIALOG,
};
}
async submit() {
// Actions.HomeScreen();
const today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
const yyyy = today.getFullYear();
const hh= today.getHours();
const min=today.getMinutes();
const sec=today.getSeconds();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
const fordate = dd+'-'+mm+'-'+yyyy;
const fordate2 = yyyy+'-'+mm+'-'+dd;
const username = 'testuser';
console.log(this.state.selected1);
const res = await api.getMarkAtt(this.state.selected1,this.fordate,this.fordate2,this.state.remarkstext,this.state.selected2,this.username);
// const status= res.status;
console.log('log'+res);
if(status==='success'){
Actions.HomeScreen();
}
}
render() {
return (
<View style={styles.container}>
<ScrollView>
<View style={{backgroundColor:'#008365',marginLeft:20,marginRight:20,marginTop:10}}>
<Text style={styles.welcome}>
{"Date:"+new Date("2017-02-13").toString()}
</Text>
</View>
<View style={{backgroundColor:'#1A8E74',marginLeft:20,marginRight:20,marginTop:2,paddingLeft:20,paddingRight:20}}>
<Text style={{color:'white',fontSize:15}}>Attendance</Text>
<Picker
style={styles.picker}
selectedValue={this.state.selected1}
onValueChange={(selected1) => this.setState({ selected1 })}
>
<Picker.Item label="--Select--" value="0" key="0" />
<Picker.Item label="O" value="1" key="1"/>
<Picker.Item label="P" value="2" key="2"/>
<Picker.Item label="A" value="3" key="3"/>
</Picker>
<Text style={{color:'white',fontSize:15}}>Reason</Text>
<Picker
style={styles.picker}
selectedValue={this.state.selected2}
onValueChange={(selected2) => this.setState({ selected2 })}>
<Picker.Item label="--Select--" value="4" key="4" />
<Picker.Item label="Weekly Off" value="5" key="5"/>
<Picker.Item label="At Store" value="6" key="6"/>
<Picker.Item label="Medical" value="7" key="7"/>
</Picker>
<Text style={{color:'white',fontSize:15}}>Remarks</Text>
<TextInput
style={styles.textInputLayout}
onChangeText={(remarkstext) => this.setState({remarkstext})}
value={this.state.remarkstext}
/>
<View style={styles.buttonView}>
<Button
onPress={this.submit}
title={this.state.buttonvalue}
color='#FF7323'>
</Button>
</View>
</View>
</ScrollView>
</View>
)
}
changeMode = () => {
const newMode = this.state.mode === Picker.MODE_DIALOG
? Picker.MODE_DROPDOWN
: Picker.MODE_DIALOG;
this.setState({mode: newMode});
};
}
“严格使用”
从“React”导入React,{Component};
const ReactNative=require('react-native');
常数{
皮克,
评估学,
样式表,
领航员,
文本,
看法
文本输入,
按钮
卷轴视图
}=反应性;
常量项=选择器项;
从“react native router flux”导入{Actions};
从“/utilities/api”导入api;
导出默认类MarkAttTab扩展组件{
建造师(道具){
超级(道具);
此.state={
备注文字:‘备注’,
按钮值:“提交”,
selected1:“--选择--”,
selected2:“--选择--”,
颜色:“红色”,
模式:Picker.mode_对话框,
};
}
异步提交(){
//动作。主屏幕();
const today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;//一月是0!
const yyy=today.getFullYear();
const hh=today.getHours();
const min=today.getMinutes();
const sec=today.getSeconds();
如果(dd
评论
this.setState({remarkstext})}
值={this.state.remarkstext}
/>
)
}
变更模式=()=>{
const newMode=this.state.mode===Picker.mode\u对话框
?选择器模式下拉菜单
:Picker.MODE_对话框;
this.setState({mode:newMode});
};
}
我想您忘记了在按钮中绑定事件
,请使用以下部分:
<Button
onPress = {this.submit.bind(this)}
title = {this.state.buttonvalue}
color = '#FF7323'>
</Button>
更新-获取
调用模式:
fetch(url)
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch( error =>
console.log('There has been a problem with your fetch operation: ' + error.message);
)
您需要以这种方式捕获异常和错误。我想您忘记了绑定按钮中的事件,请使用此部分:
<Button
onPress = {this.submit.bind(this)}
title = {this.state.buttonvalue}
color = '#FF7323'>
</Button>
更新-获取
调用模式:
fetch(url)
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch( error =>
console.log('There has been a problem with your fetch operation: ' + error.message);
)
您需要以这种方式捕获异常和错误。我做了此更改,不再收到此错误,但现在可能会收到未经处理的承诺拒绝(id:1):JSON解析错误:意外标识符“无法”解析@[本机代码]您是否使用api
调用定义了Catch
,像这样:.catch(函数(错误){..
?检查这个:你的意思是在这里吗?这在api.js文件中..getMarkAtt(考勤、fordate、date2Pass、备注、原因、用户名){var url='url';return fetch(url).然后((res)=>res.json();),仅限此处为“是”,检查上一个注释链接问题的答案,如何捕获异常。同时检查更新的答案。“返回语句将转到何处?我在捕获块行上得到了意外的标记(预期)。我做了此更改,我不再收到此错误,但现在我可能会收到未经处理的承诺拒绝(id:1):JSON解析错误:意外标识符“无法”解析@[本机代码]您是否使用api
调用定义了catch
,如下:.catch(函数(错误){..
?检查:您的意思是在这里?这在api.js文件中..getMarkAtt(出席、fordate、date2Pass、备注、原因、用户名){var url='url',返回fetch(url).then((res)=>res.json());},仅在此处是,检查上一个注释链接问题的答案,如何捕获异常。同时检查更新的答案。'return语句将去哪里?我在catch块行上得到了意外的标记,预期