undefined不是对象(评估'RNDeviceInfo.deviceId') - react-native-device-info中的错误

时间:2016-07-10 10:43:29

标签: javascript android react-native

我正在使用 react-native-device-info 库,它适用于iOS模拟器。但是对于Android我收到一个错误:

id

可能是什么问题? 我安装它就像库的页面上的指南(使用rnpm)中描述的那样。 然后在componentWillMount()中我试图获取设备ID:

undefined is not object (evaluating 'RNDeviceInfo.deviceId')

5 个答案:

答案 0 :(得分:2)

除了rnpm install react-native-device-info和来自here的说明外,我还必须手动为以下文件添加一些行:

  1. <YourReactProject>/android/settings.gradle

    rootProject.name = 'YourReactProject'
    
    include ':app', ':react-native-device-info'
    project(':react-native-device-info').projectDir = newFile(rootProject.projectDir, '../node_modules/react-native-device-info/android')
    
  2. <YourReactProject>/android/app/build.gradle

    ...
    
    dependencies {
       compile fileTree(dir: "libs", include: ["*.jar"])
       compile "com.android.support:appcompat-v7:23.0.1"
       compile "com.facebook.react:react-native:+"  // From node_modules
       compile project(':react-native-device-info')
    }
    
    ...
    
  3. 运行react-native run-android

  4. 重建

答案 1 :(得分:0)

首先检查它是否已添加到package.json中? 然后使用以下命令

链接包和react native
react-native link react-native-device-info

它使用react-native手动链接包。

我在IOS面临这个问题。

即使我链接显示相同问题的包。

在Xcode中打开你的项目, 然后在构建阶段,通过单击+按钮添加react-native-device-info的二进制文件。

重新运行项目,它对我有用。

enter image description here

答案 2 :(得分:0)

该包装现在似乎正在使用Cocoapods。运行react-native link,然后检查您的Podfile中是否存在类似内容:

pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'

如果在那里,那么您只需安装/更新您的Pod。

cd ios
pod install

cd ios
pod update

确保重新启动Metro服务器并重新运行react-native run-ios

答案 3 :(得分:0)

使用manual installation并更改

  

您的项目\ node_modules \ react-native-device-info \ android \ build.gradle'

dependencies {
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION

compile 'com.facebook.react:react-native:+'
compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"}

在这里以编译方式实现

答案 4 :(得分:0)

首先,如果您使用的是本机版本<0.60。

,请确保在项目中链接了react-native-device-info

如果未链接,请使用react-native link react-native-device-info将其链接,然后运行react-native run-android命令。

如果使用本机版本> = 0.60 ,请跳过此链接命令,因为自动链接应该可行,但请确保运行react-native run-android命令。

然后使用,

从“ react-native-device-info”导入DeviceInfo;

DeviceInfo.getUniqueId()

有一个小错字。您正在使用 DeviceInfo.getUniqueID(); 而不是 DeviceInfo.getUniqueId()

在文档here中查看