3

Is there a way to make so the barcode scanner checks for the barcode number in my local database? I've been trying to do some searches on google but there is no tutorials for something like this.


import DataBase from '../Data/DataBase';

class BarCodeReader extends Component {
  constructor(props) {
    super(props);
    this.camera = null;
    this.barcodeCodes = [];

    this.state = {
      camera: {
        type: RNCamera.Constants.Type.back,
        flashMode: RNCamera.Constants.FlashMode.auto,
      },
    };
  }

  onBarCodeRead(scanResult) {
    if (scanResult.data != null) {
      if (!this.barcodeCodes.includes(scanResult.data)) {
        this.barcodeCodes.push(scanResult.data);
        alert(scanResult.data);
      }
    }
    return;
  }

  render() {
    return (
      <View style={styles.container}>
        <RNCamera
          ref={(ref) => {
            this.camera = ref;
          }}}
          onBarCodeRead={this.onBarCodeRead.bind(this)}
        />
      </View>
    );
  }
}

const DataBase = [
  {
    name: 'text',
    company: 'text',
    extra: '7 310350 118670',
  },
];
export default DataBase;
Lmao12233
  • 61
  • 1
  • 1
  • 15
  • 30 seconds of googling for you :) [tutorial](https://medium.com/@dinukadilshanfernando/implementing-a-barcode-scanner-by-using-react-native-camera-b170de4b7f51) – Robert Jan 07 '21 at 13:24
  • So ask how to "fetch data from database..." this what you looking for. if know how to connect to database and fetch what you need then is no matter what you check. So read how to build api which will connect to database and send to your app what you need. (REST API) – Robert Jan 07 '21 at 14:00
  • This app is only local and will not have a server. – Lmao12233 Jan 07 '21 at 14:16
  • So what local database you use ? Realm ? Firebase ?PouchDB? SQlite? – Robert Jan 07 '21 at 15:17
  • [Realm tutorail](https://aboutreact.com/example-of-realm-database-in-react-native/) – Robert Jan 07 '21 at 15:20
  • This is my bad with local i mean its a local file and u can see how its setup above – Lmao12233 Jan 07 '21 at 15:30
  • So summary. You don't use database, you don't have problem with barcode reading. Just your question don't have nothing common with your problem. You have just pre configured data in json format and that's all. What you expect to happen when you figure out is 'code' correct on not ? ....just use array.filter. nothing more. but this code is just useless without possibility to add new codes... and to do it you have to use some local database or at least local storage. – Robert Jan 07 '21 at 17:13

1 Answers1

0
import codes from '../Data/codes';

class BarCodeReader extends Component {
  constructor(props) {
    super(props);
    this.camera = null;
    this.barcodeCodes = [];

    this.state = {
      camera: {
        type: RNCamera.Constants.Type.back,
        flashMode: RNCamera.Constants.FlashMode.auto,
      },
    };
  }
  
  validateCode(scanResult){
    const codeData = codes.find(codeMetadata => {
       return codeMetadata.id === scanResult.data 
    })
    
    if(codeData){
      alert("code valid" + codeData)
    } else {
      alert("code not found")
    }
  }

  onBarCodeRead(scanResult) {
    if (scanResult.data != null) {
      if (!this.barcodeCodes.includes(scanResult.data)) {
        this.barcodeCodes.push(scanResult);
        this.validateCode(scanResult)
      }
    }
    return;
  }

  render() {
    return (
      <View style={styles.container}>
        <RNCamera
          ref={(ref) => {
            this.camera = ref;
          }}}
          onBarCodeRead={this.onBarCodeRead.bind(this)}
        />
      </View>
    );
  }
}

const codes = [
  {
    id: "code-id-text"
    name: 'text',
    company: 'text',
    extra: '7 310350 118670',
  },
Robert
  • 2,538
  • 1
  • 9
  • 17