tr3v3r / react-native-esc-pos-printer

An unofficial React Native library for printing on an EPSON TM printer with the Epson ePOS SDK for iOS and Epson ePOS SDK for Android
MIT License
157 stars 72 forks source link

Error: [TypeError: Cannot read property 'target' of undefined] #50

Closed davmaene closed 1 year ago

davmaene commented 2 years ago

when I try to run the function to print, an error occurs telling me that Error: [TypeError: Cannot read property 'target' of undefined] and when i try to console the printers variable it returns me an empty array here is my code

const PrintPOS = async () => {
        try {
            const printers = await EscPosPrinter.discover()

            const printer = printers[0]

            console.log(printers);

            await EscPosPrinter.init({
                target: printer.target,
                seriesName: getPrinterSeriesByName(printer.name),
                language: 'EPOS2_LANG_EN',
            })

            const printing = new EscPosPrinter.printing();

            const status = await printing
              .initialize()
              .align('center')
              .size(3, 3)
              .line('DUDE!')
              .smooth()
              .line('DUDE!')
              .smooth()                
              .size(1, 1)
              .text('is that a ')
              .bold()
              .underline()
              .text('printer?')
              .bold()
              .underline()
              .newline(2)
              .align('center')
            //   .image(image, 200)
              .barcode({
                value:'Test123',
                type:'EPOS2_BARCODE_CODE93',
                hri:'EPOS2_HRI_BELOW',
                width:2,
                height:50,
              })
              .qrcode({
                value: 'Test123',
                level: 'EPOS2_LEVEL_M',
                width: 5,
              })
              .cut()
              .addPulse()
              .send()

              console.log('Success:', status)

        } catch(e) {
              console.log('Error:', e)
        }
    }

i also add this req. permission to my android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
ArifSanaullah commented 2 years ago

when I try to run the function to print, an error occurs telling me that Error: [TypeError: Cannot read property 'target' of undefined] and when i try to console the printers variable it returns me an empty array here is my code

const PrintPOS = async () => {
        try {
            const printers = await EscPosPrinter.discover()

            const printer = printers[0]

            console.log(printers);

            await EscPosPrinter.init({
                target: printer.target,
                seriesName: getPrinterSeriesByName(printer.name),
                language: 'EPOS2_LANG_EN',
            })

            const printing = new EscPosPrinter.printing();

            const status = await printing
              .initialize()
              .align('center')
              .size(3, 3)
              .line('DUDE!')
              .smooth()
              .line('DUDE!')
              .smooth()                
              .size(1, 1)
              .text('is that a ')
              .bold()
              .underline()
              .text('printer?')
              .bold()
              .underline()
              .newline(2)
              .align('center')
            //   .image(image, 200)
              .barcode({
                value:'Test123',
                type:'EPOS2_BARCODE_CODE93',
                hri:'EPOS2_HRI_BELOW',
                width:2,
                height:50,
              })
              .qrcode({
                value: 'Test123',
                level: 'EPOS2_LEVEL_M',
                width: 5,
              })
              .cut()
              .addPulse()
              .send()

              console.log('Success:', status)

        } catch(e) {
              console.log('Error:', e)
        }
    }

i also add this req. permission to my android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Got the same error in a new react-native project. Any solution so far?

davmaene commented 2 years ago

yes, I already have a solution, thank you very much

DenisFjordfox commented 2 years ago

@davmaene What's your solution?

davmaene commented 2 years ago

1 : you must first make sure that the bluetooth connection is activated. 2 : then you need to make sure there are bluetooth devices ( your printer is on ) 3 : but if the problem persists I advise you to use this other module (react-native-pos-printer) this one work well

here there are some lines of code that i had used in a project

import * as React from 'react';
import { View, Text, TouchableHighlight, Image } from 'react-native';
import { FooterScreen } from '../../components/Footer/FooterComponent';
import { Head } from '../../components/Head/HeadComponent';
import { Consts } from '../../assets/scripts/AppConstants';
import CheckBox from 'react-native-check-box';
import RNPosPrinter from 'react-native-pos-printer';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { ScrollView } from 'react-native-gesture-handler';

export const PrinterScreen = ({navigation, route}) => {

    const [permi, setpermi] = React.useState(false);
    const [vignete, setvignet] = React.useState(false);
    const [rose, setrose] = React.useState(false);
    const [ass, setass] = React.useState(false);
    const [elec, setelect] = React.useState(false);

    let infos = null;
    let infractions = null;

    if(route && route['params'] && route['params']['infos']) infos = route['params']['infos'];
    if(route && route['params'] && route['params']['infractions']) infractions = route['params']['infractions'];

    const PrintPOS = async () => {
        try {
            RNPosPrinter.init(true).then(d => {
                console.log("done");
            })
            .catch(e => {
                console.log("error ");
            })
            RNPosPrinter.getDevices().then(d => {
                console.log("done getting devices", d);
            })
            .catch(er => {
                console.error("error", er);
            })
            RNPosPrinter.scanDevices((e) => {
                console.error(e);
            })
            RNPosPrinter.printTestReceipt("david maene");

        } catch(e) {
            console.error(e);
        }
    }

    return(
        <>
           .....
           ....
           ....
        </>
   )
}
DenisFjordfox commented 2 years ago

@tr3v3r Do you have any thoughts about it?

tr3v3r commented 2 years ago

@DenisFjordfox hi! Could you please share more info. platform, lib version, Android version, iOS version, printer model, type of connection (lan, Bluetooth and etc.)

DenisFjordfox commented 2 years ago

@tr3v3r I'm using the latest lib version - 1.11.0.

Printer: Epson TM-P80 via bluetooth Device: Galaxy A51, Android 11 React Native: 0.63.4

tr3v3r commented 2 years ago

@DenisFjordfox have you added this permission?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION

DenisFjordfox commented 2 years ago

@tr3v3r Yeah. Permissions are the following:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
tr3v3r commented 2 years ago

@DenisFjordfox have you tried to increase scanningTimeoutAndroid ? Is you printer properly been configured? I mean have you tried to print something using another software?

DenisFjordfox commented 2 years ago

@tr3v3r Yeah, was setting timeout to bigger values, but the issue persists. I have only one printer available, so cannot check it properly. I think, that I'll try to setup the library on a bare RN project to check the behavior

tr3v3r commented 2 years ago

@DenisFjordfox I’m asked not about another printer but about another software to connect and print on your TM-P80 printer. I.e. did you manage to print something from pc via Bluetooth connection?