【问题标题】:TypeError: undefined is not an object (evaluating 'BMDPedometer.isStepCountingAvailable')TypeError:未定义不是对象(评估'BMDPedometer.isStepCountingAvailable')
【发布时间】:2019-01-20 07:53:36
【问题描述】:

我是 React-Native 的新手,正在构建我的第一个应用程序。我正在尝试使用这个library 来构建一个简单的计步应用程序。我做了 npm install 和 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);

【问题讨论】:

  • 你执行react-native link了吗?
  • GitHub 上已经有一个 issue 与库。看起来,PR 可以修复它,因为错误表明没有匹配的本机模块。这是图书馆里的东西。
  • @riwu 是的,我做到了

标签: javascript android react-native pedometer


【解决方案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

这将采用不再存在依赖项的 repo 的主版本。

我已经创建了一个example repository,您可以看看我在哪里可以成功调用isStepCountingAvailable 而没有任何错误here

请注意,我使用yarn 来管理我的部门,但您显然可以使用npm 代替

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2021-12-01
    • 2019-11-16
    • 2020-02-09
    • 2019-12-07
    • 2022-01-01
    • 2014-06-13
    • 2021-07-24
    • 2016-09-02
    相关资源
    最近更新 更多