简体   繁体   中英

undefined is not an object when import external js file in react native

这是什么错误 i am import external js file and call funcation in component but getting error "get i use this code but showing undefined is not an object (evaluating _ApiHelper.ApiHelper.getPost)"

Main login file code

import React, { Component } from "react";
import Icon from "react-native-vector-icons/Ionicons";
import {
  View,
  StyleSheet,
  Image,
  TextInput,
  TouchableOpacity,
  Text,
  Alert,
  KeyboardAvoidingView
} from "react-native";
//
import { Search } from '../Search/Search';
import { ApiHelper } from '../../api/ApiHelper'; 

export default class Login extends Component {
  static navigationOptions =
   {
      title: 'LoginActivity',
   };

constructor(props) {

    super(props);
    //
    //Obj = new ApiHelper();
    //

    this.state = {
      userName: '',
      password: '',
      serverName:''
    }

  }

UserRegistrationFunction = () =>{


 const { userName }  = this.state ;
 const { password }  = this.state ;
 const { serverName }  = this.state ;
  this.data =  {
      name: userName,
      email: password,
      password: serverName

    }

   alert(ApiHelper.getPost());
 //Alert.alert(returndata);

  }

Import file code

  const ApiHelper = {
  getPost:() => {
    return 1;

  }
}

export default ApiHelper;

what wrong in my code i also clear react native cache and other stuff. thank in advance.

Your ApiHelper file exports default .

So in order to use it somewhere else, just import it as:

import ApiHelper from '../../api/ApiHelper'

I guess getData is not defined/misspelled in the ApiHelper class and the function working on the output expects an object. Since typeof(undefined) is not object that's why it is throwing an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM