TypeError:未定义不是对象(正在评估“ BMDPedometer.isStepCountingAvailable”)

时间:2018-08-13 14:33:29

标签: javascript android react-native pedometer

我是React-Native的新手,正在构建我的第一个应用程序。我正在尝试使用此library来构建一个简单的计步应用程序。我做了npm安装,还有react-natvie链接。我正在尝试以与页面上提到的库所有者相同的方式进行复制。

但仍然出现以下错误:

TypeError: undefined is not an object (evaluating 
'BMDPedometer.isStepCountingAvailable')
: This error is located at:
in StepsCounter (at App.js:26)
in App (at renderApplication.js:32)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in AppContainer (at renderApplication.js:31)
TypeError: undefined is not an object (evaluating'BMDPedometer.isStepCountingAvailable')

这是我的StepsCounter.android.js

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import Pedometer from '@asserdata/react-native-universal-pedometer';

export default class StepsCounter extends Component{

constructor(){
    Pedometer.isStepCountingAvailable((error,isAvailable)=>{
if(error) throw error;
        console.log("Working");
    })
}

render()
{
    return(
    <View>
        <Text> Brooooo</Text>
    </View>
    );
}
    }

这是我的App.js

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,AppRegistry} from 'react-native';
import ReactNative from 'react-native';
import Pedometer from '@asserdata/react-native-universal-pedometer';
import StepsCounter from './components/stepscounter';

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {
render() {
return (
  <StepsCounter/>
);
}
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

// ReactNative.AppRegistry.registerComponent('StepsCounter',()=> App);

1 个答案:

答案 0 :(得分:1)

首先,有一个问题是您正在使用实际软件包react-native-universal-pedometer的未维护的叉子。您将需要删除添加的链接并将其删除:

react-native unlink @asserdata/react-native-universal-pedometer
yarn remove @asserdata/react-native-universal-pedometer

接下来,由于react-native-universal-pedometer还具有an issue,因为它们包括react-native作为软件包的依赖项,而不是对等的dep,您很可能最终会遇到版本冲突。

在等待维护者发布包含this fix的新npm版本时,您可以直接从Github安装该软件包:

yarn add smekalka/react-native-universal-pedometer
react-native link

这将使用不再依赖的仓库的主版本。

我做了一个example repository,您可以看看我在哪里能够成功致电isStepCountingAvailable而没有任何错误here

请注意,我正在使用yarn来管理部门,但是显然可以使用npm