簡體   English   中英

TypeError: undefined is not a function ('...this.state.profile.map...'附近)

[英]TypeError: undefined is not a function (near '…this.state.profile.map…')

我一次又一次地收到錯誤。 我不知道為什么我會收到這個錯誤。

我得到的響應也是一個數組,我嘗試使用 console.log。 下面是證明

來自 axios Api 的響應:

{

"CCompany": "Testing Company", 
"CFName": "Rehan", 
"CLName": "ahmed", 
"CMName": "", 
"CTelHome": "1-232-2323232", 
"UID": "700002"

}

下面是代碼:

import React, { Component } from 'react';

import { View, Text, Dimensions, BackHandler, ToastAndroid } from 'react-native';

import axios from 'axios';

import Card from './Card';

import CardSection from './CardSection';

import ProfileDetails from './ProfileDetails';

import AsyncStorage from '@react-native-community/async-storage';



// Create a component

class ProfileActivity extends Component {

    constructor() {
        super();

        this.state = {
            profile: [],
            setUID: '',
            isloading: true,
        };
    }

    state = {
        canBeClosed: false
    }


    componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
    }

    handleBackButton = () => {
        if (this.props.navigation.isFocused()) {
            if (this.state.canBeClosed)
                return this.state.canBeClosed = false;

            else {
                setTimeout(() => { this.state.canBeClosed = false }, 3000);
                ToastAndroid.show("Press Again To Exit !", ToastAndroid.LONG);

                return this.state.canBeClosed = true
            }
        }
    };

    async componentDidMount() {
       
        try {
            if (this.state.setUID == null && this.state.profile == null) {
                console.log('profile remove');

                const user = await AsyncStorage.getItem('responseJson');
                const parsed = JSON.parse(user);
                if (parsed !== null) {
                    this.setState({ setUID: parsed.UID });
                }
               
                axios.get('https:/new.didx.net/didxapi/UserInfo.php?UID=' + this.state.setUID)
                    .then(response => this.setState({ profile: response.data }));
            }
            else {
                this.setState({ setUID: "" });
                this.setState({ profile: "" });
                console.log('not remove');
                const user = await AsyncStorage.getItem('responseJson');
                const parsed = JSON.parse(user);
                if (parsed !== null) {
                    this.setState({ setUID: parsed.UID });
                }
               
                axios.get('https:/new.didx.net/didxapi/UserInfo.php?UID=' + this.state.setUID)
                    .then(response => this.setState({ profile: response.data }));
            }
        }
        catch (error) {
            alert('Server Error!')
        }

        BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
       

    }

    renderProfile() {
        if (this.state.profile) {
            console.log(this.state.profile);
            return this.state.profile.map(profile => (
                <ProfileDetails  key={profile.UID} profile={profile} />
             ));
        }
    }


    render() {
        return (

            <View style={styles.container}>
                {this.renderProfile()}

            </View>
        );
    }
}

export default ProfileActivity;

const h = Dimensions.get('screen').height * 0.01;
const w = Dimensions.get('screen').width * 0.01;


const styles = {
    container: {
        flex: 1,
        backgroundColor: '#fff'
    },
    ViewStyle: {
        paddingTop: h * 5,

    },
    TextStyle: {
        justifyContent: 'flex-start',
        // alignSelf: 'center',
        color: '#000',
        fontWeight: 'bold',
        fontSize: 20,
        padding: 5,
        fontFamily: 'Roboto',
        maxWidth: w * 50,

    }
}

我盡我所能來解決這個問題。

.map expects an array... but your axios.get('https:/new.didx.net/didxapi/UserInfo.php?UID=' + this.state.setUID) call returns an object like { UID: "1", CFName: "1", CMName: "", CLName: "1", CCompany: "1", CTelHome: "2" }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM