简体   繁体   English

在React Native中导入外部js文件时,undefined不是对象

[英]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)" 我正在导入外部js文件并在组件中调用函数,但收到错误消息“让我使用此代码,但显示未定义不是对象(评估_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 . 您的ApiHelper文件导出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. 我想getData在ApiHelper类中没有定义/拼写错误,并且在输出上工作的函数需要一个对象。 Since typeof(undefined) is not object that's why it is throwing an error. 由于typeof(undefined)不是对象,因此它会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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