CalcGP.semester 必须是“数字”类型,得到“字符串”(“1”)

Posted

技术标签:

【中文标题】CalcGP.semester 必须是“数字”类型,得到“字符串”(“1”)【英文标题】:CalcGP.semester must be of type 'number', got 'string' ('1') 【发布时间】:2018-11-18 12:09:28 【问题描述】:

`import React, Component from 'react';

import StyleSheet, Platform, View, Image, Text, TextInput, TouchableOpacity, Alert, YellowBox, ListView from 'react-native';

const Realm = require('realm');

让领域;

从 'react-navigation' 导入 StackNavigator ;

类 MainActivity 扩展组件

static navigationOptions =
    
        title: 'MyGPA',
    ;

GoToSecondActivity = () =>

    this.props.navigation.navigate('Second');

;

constructor()

    super();

    this.state = 

        Student_Name : '',

        Semester : '',


        GPA : ''

    ;

    realm = new Realm(
        schema: [name: 'CalcGP',
            properties:
                
                    student_id: type: 'int',   default: 0,
                    student_name: 'string',
                    semester: 'int',
                    gpa: 'double'
                ]
    );

    YellowBox.ignoreWarnings([
        'Warning: componentWillMount is deprecated',
        'Warning: componentWillReceiveProps is deprecated',
        'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
    ]);



add_Student=()=>


    realm.write(() => 

        let ID = realm.objects('CalcGP').length + 1;

        realm.create('CalcGP', 
            student_id: ID,
            student_name: this.state.Student_Name,
            semester: this.state.Semester,
            gpa : this.state.GPA,

        );

    );
    Alert.alert("Details Added Successfully.");

;


render() 

    return (

        <View style=styles.MainContainer>

            <TextInput
                placeholder="Enter Student Name"
                style =  styles.TextInputStyle 
                underlineColorandroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( Student_Name: text ) 
            />

            <TextInput
                placeholder="Enter Semester"
                style =  styles.TextInputStyle 
                underlineColorAndroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( Semester: text ) 
            />

            <TextInput
                placeholder="Enter GPA"
                style =  styles.TextInputStyle 
                underlineColorAndroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( GPA: text ) 
            />


            <TouchableOpacity onPress=this.add_Student activeOpacity=0.7 style=styles.button >

                <Text style=styles.TextStyle> SAVE TO DATABASE </Text>

            </TouchableOpacity>



            <TouchableOpacity onPress=this.GoToSecondActivity activeOpacity=0.7 style=styles.button >

                <Text style=styles.TextStyle> SHOW MY GPA </Text>

            </TouchableOpacity>

        </View>

    );

类 ShowDataActivity 扩展组件 静态导航选项 = 标题:“我的GPA”, ;

constructor() 

    super();
    this.state = 

        Student_Name : '',

        Semester : '',


        GPA : ''

    ;

    realm = new Realm(
        schema: [name: 'CalcGP',
            properties:
                
                    student_id: type: 'int',   default: 0,
                    student_name: 'string',
                    semester: 'int',
                    gpa: 'double'
                ]
    );

    YellowBox.ignoreWarnings([
        'Warning: componentWillMount is deprecated',
        'Warning: componentWillReceiveProps is deprecated',
        'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
    ]);

    let mydata = realm.objects('CalcGP');

    let ds = new ListView.DataSource(rowHasChanged: (r1, r2) => r1 !== r2);

    this.state = 
        dataSource: ds.cloneWithRows(mydata),
    ;



GoToEditActivity (student_id, student_name, semester, gpa) 

    this.props.navigation.navigate('Third', 

        ID : student_id,
        NAME : student_name,
        CLASS : semester,
        SUBJECT : gpa,

    );



dbtotal() 
    //allGP = realm.objects(GP);
    let cgpa = realm.objects('CalcGP').avg(gpa) ;
    Alert.alert(cgpa);
    //let tanDogs = dogs.filtered('color = "tan" AND name BEGINSWITH "B"');


ListViewItemSeparator = () => 
    return (
        <View
            style=
                height: .5,
                width: "100%",
                backgroundColor: "#000",
            
        />
    );
;

render()

    return(

    <View style =  styles.MainContainer >

        <ListView

            dataSource=this.state.dataSource

            renderSeparator= this.ListViewItemSeparator

            renderRow=(rowData) => <View style=flex:1, flexDirection: 'column' >

                <TouchableOpacity onPress=this.GoToEditActivity.bind(this, rowData.student_id, rowData.student_name, rowData.semester, rowData.gpa) >

                    <Text style=styles.textViewContainer >'id = ' + rowData.student_id</Text>

                    <Text style=styles.textViewContainer >'Name = ' + rowData.student_name</Text>

                    <Text style=styles.textViewContainer >'Semester = ' + rowData.semester</Text>

                    <Text style=styles.textViewContainer >'GPA = ' + rowData.gpa</Text>

                </TouchableOpacity>


            </View> 

        />
        <TouchableOpacity onPress=this.dbtotal activeOpacity=0.7 style=styles.button >

            <Text style=styles.TextStyle> CALCULATE </Text>

        </TouchableOpacity>
    </View>
    );

类 EditActivity 扩展组件

static navigationOptions =
    
        title: 'EditActivity',
    ;


constructor() 

    super();
    this.state = 

        Student_Id : '',

        Student_Name: '',

        Semester: '',

        GPA: ''

    ;


    YellowBox.ignoreWarnings([
        'Warning: componentWillMount is deprecated',
        'Warning: componentWillReceiveProps is deprecated',
        'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
    ]);








componentDidMount()

    // Received Student Details Sent From Previous Activity and Set Into State.

    this.setState(
        Student_Id : this.props.navigation.state.params.ID,
        Student_Name: this.props.navigation.state.params.NAME,
        Semester: this.props.navigation.state.params.CLASS,
        GPA: this.props.navigation.state.params.SUBJECT
    )


Update_Student=()=>

    realm.write(() => 

        const ID = this.state.Student_Id - 1;

        const obj = realm.objects('CalcGP');

        obj[ID].student_name = this.state.Student_Name;
        obj[ID].semester = this.state.Semester;
        obj[ID].gpa = this.state.GPA;

    );

    Alert.alert("GP Updated Successfully.")

;

Delete_Student=()=>

    realm.write(() => 

        const ID = this.state.Student_Id - 1;

        realm.delete(realm.objects('CalcGP')[ID]);

    );

    Alert.alert("Record Deleted Successfully.");

    this.props.navigation.navigate('Show');

;
render() 

    return (

        <View style=styles.MainContainer>

            <TextInput
                value=this.state.Student_Name
                style =  styles.TextInputStyle 
                underlineColorAndroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( Student_Name: text ) 
            />

            <TextInput
                value=this.state.Semester
                style =  styles.TextInputStyle 
                underlineColorAndroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( Semester: text ) 
            />

            <TextInput
                value=this.state.GPA
                style =  styles.TextInputStyle 
                underlineColorAndroid = "transparent"
                onChangeText =  ( text ) =>  this.setState( GPA: text ) 
            />


            <TouchableOpacity onPress=this.Update_Student activeOpacity=0.7 style=styles.button >

                <Text style=styles.TextStyle> CLICK HERE TO UPDATE GP DETAILS </Text>

            </TouchableOpacity>

            <TouchableOpacity  activeOpacity=0.7 style=styles.button onPress=this.Delete_Student >

                <Text style=styles.TextStyle> CLICK HERE TO DELETE CURRENT RECORD </Text>

            </TouchableOpacity>

        </View>

    );

【问题讨论】:

【参考方案1】:

使用这部分代码:

<TextInput
    placeholder="Enter Semester"
    style =  styles.TextInputStyle 
    underlineColorAndroid = "transparent"
    onChangeText =  ( text ) =>  this.setState( Semester: Number(text) ) 
/>

【讨论】:

检查您的代码并将 this.setState( Semester: text ) 替换为 this.setState( Semester: Number(text) )。您将学期定义为整数并将字符串传递给它。默认情况下,来自 textinput 的文本是字符串,您必须将其转换为整数。 我需要得到所有 gpa 字段的平均值,我试过了,但它不起作用 let cgpa: Double = realm.objects('CalcGP').avg(gpa) ;警报.alert(cgpa);它告诉我未解决的变量 Double

以上是关于CalcGP.semester 必须是“数字”类型,得到“字符串”(“1”)的主要内容,如果未能解决你的问题,请参考以下文章

计算属性名称必须是“字符串”、“数字”、“符号”或“任意”类型

Vimeo Player API 提示点事件:未捕获类型错误:时间必须是数字

DbArithmeticExpression 参数必须具有数字通用类型

Hibernate Validator怎么验证int类型数据(就是说比如输入中文就提示必须是数字)

“输入类型为带有正确数字的“数字”必须输入有效值

java 金融数字用啥类型