【问题标题】:TypeError: undefined is not a function (near '...this.state.profile.map...')TypeError: undefined is not a function ('...this.state.profile.map...'附近)
【发布时间】:2021-02-16 23:25:55
【问题描述】:

我一次又一次地出错。我不知道为什么会出现这个错误。

我得到的响应也是一个数组,我尝试使用 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,

    }
}

我尽我所能来解决这个问题。

【问题讨论】:

  • 看起来你定义了两次初始状态,一次在构造函数中,一次在构造函数之外。我想知道这是否是问题的原因?
  • @Muhammad ...您在问题中的响应样本不是数组...它是对象

标签: javascript reactjs react-native


【解决方案1】:

.map 需要一个数组...但是您的 axios.get('https:/new.didx.net/didxapi/UserInfo.php?UID=' + this.state.setUID) 调用返回一个 object 就像 { UID: "1", CFName: "1", CMName: "", CLName: "1", CCompany: "1", CTelHome: "2" } 一样

【讨论】:

  • 我在浏览器中放置了一个类似 https:/new.didx.net/didxapi/UserInfo.php?UID=1 的端点,我得到了一个对象......这就是你在 RN 中遇到的.map 问题......我想你需要修复你的后端 - api
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 2021-06-21
  • 1970-01-01
  • 2014-08-15
  • 2015-01-14
相关资源
最近更新 更多