3

what's this error 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.

4
  • Either you're importing the wrong file, or you're not showing the relevant part of the code. Or maybe you didn't copy/paste the error and made a typo. Commented Jan 5, 2018 at 9:39
  • i am import right file Commented Jan 5, 2018 at 9:41
  • The error shows a call to getdata, which is not shown here in your code. Commented Jan 5, 2018 at 9:42
  • 1
    Turns out you made a typo. :) Now look at @mersocarlin's answer. Commented Jan 5, 2018 at 9:45

2 Answers 2

4

Your ApiHelper file exports default.

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

@jhon Did you remove the curly brackets as well?
yes i remove used like import ApiHelper from '../../api/ApiHelper';
What happens if you console.log(ApiHelper)? (without calling getPost)
get [object Object]
0

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.

1 Comment

sorry for getData it getPost i am using and it not working i didn't understand what wrong in my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.