Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "TypeError: undefined is not an object (evaluating '_reactNativeCamera.default.constants')" error in react native?

I'm trying to use "React-native-camera" library in my project. I've literally done this over 50 times and everything went fine each time both on my mac and Linux System.

But now out of a sudden it wont work on my Linux System anymore and keeps generating this error (works on mac though!).

enter image description here

I've checked all the configuring steps here I've tried adding missingDimensionStrategy 'react-native-camera', 'general' to app build.gradle I've tried adding maven {url "https://jitpack.io"} and maven {url "https://maven.google.com"} to project build.gradle I've tried removing madules.xml from .ideas directory and restarting android studio both with and without cache clearing.

still nothing This was my sample code to get the camera module going:

class CameraApp extends Component {
  constructor(props) {
    super(props);

    this.state = {
      path: null,
      uri: '',
      imageName: ''
    };
  }

  takePicture() {
    this.camera.capture()
      .then((data) => {
        console.log(data);  
      })
      .catch(err => console.error(err));
}

  renderCamera() {
    return (
      <View>
        <Camera
          ref={(cam) => {
            this.camera = cam;
          }}
          style={styles.preview}
          aspect={Camera.constants.Aspect.fill}
          captureTarget={Camera.constants.CaptureTarget.disk}
        >
          <TouchableHighlight
            style={styles.capture}
            onPress={this.takePicture.bind(this)}
            underlayColor="rgba(255, 255, 255, 0.5)"
          >
            <View />
          </TouchableHighlight>
        </Camera>
      </View>
    );
  }


  render() {
    return (
      <View style={styles.container}>
        {this.renderCamera()}
      </View>
    );
  }
};
like image 532
ardn92 Avatar asked May 04 '19 12:05

ardn92


2 Answers

There are three issues here:

First, the import should be like:

import {RNCamera as Camera}  from 'react-native-camera';

Second, the constants must have the first letter capitalized: Constants

Third, both Aspect.fill and CaptureTarget.disk are no longer part of the Constants. So you should delete the following two lines:

  aspect={Camera.constants.Aspect.fill}
  captureTarget={Camera.constants.CaptureTarget.disk}

And you may check for the updated alternatives of those constants at there official documentation at https://react-native-community.github.io/react-native-camera/docs

like image 117
Muhammad Altabba Avatar answered Sep 18 '22 10:09

Muhammad Altabba


I found this link, maybe helpful to you (it work for me): Issue #1288 react-native-camera

You should import like this:

import { RNCamera } from 'react-native-camera';

Not like this

import Camera from 'react-native-camera';

like image 32
fnaquira Avatar answered Sep 19 '22 10:09

fnaquira