ansontio / esc-pos-encoder-ionic-demo

An Ionic 5 demo application to print a thermal receipt through bytcode.
MIT License
30 stars 11 forks source link

Page cannot access when use "esc-pos-encoder-ionic" #8

Open KApoNG21 opened 3 years ago

KApoNG21 commented 3 years ago

Hi, I try to use "esc-pos-encoder-ionic" NPM package in my Ionic project. When I try to open the page which use the service contains the imported library I have this error:

ERROR Error: Uncaught (in promise): ReferenceError: Cannot access 'HomePageModule' before initialization ReferenceError: Cannot access 'HomePageModule' before initialization

Related code:

import EscPosEncoder from 'esc-pos-encoder-ionic'; //---- createTicket(){ const encoder = new EscPosEncoder(); const result = encoder.initialize();

result
  .align('center')
  .newline()
  .line('Congratulation, print success')
  .line('IP : ' + '192.168.1.1')
  .line('Port : ' + '9100')
  .newline()
  .newline()
  .newline()
  .newline()
  .newline()
  .newline()
  .cut();

const resultByte = result.encode();

// send byte code into the printer
socket.open(
  '192.168.1.1',
  '9100',
  () => {
    socket.write(resultByte, () => {
      socket.shutdownWrite();
    });
  },
  (err) => {
    console.error(err);
  }
);

}

ansontio commented 3 years ago

Have you defined the socket?

Please refer to https://github.com/Ans0n-Ti0/esc-pos-encoder-ionic-demo/blob/main/EscPosDemo/src/app/tab3/tab3.page.ts

KApoNG21 commented 3 years ago

Have you defined the socket?

Please refer to https://github.com/Ans0n-Ti0/esc-pos-encoder-ionic-demo/blob/main/EscPosDemo/src/app/tab3/tab3.page.ts

Yes. I declare socket like these code.

import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx'; import EscPosEncoder from 'esc-pos-encoder-ionic';

declare var BTPrinter: any; declare let window: any; declare var Socket: any;

POSPrint(){
const socket = new Socket(); /* // socket receive bytecode, therefore we need to create a byte stream by using esc-pos-encoder-ionic const encoder = new EscPosEncoder(); const result = encoder.initialize();

result
  .align('center')
  .newline()
  .line('Congratulation, print success')
  .line('IP : ' + this.printer.mIp)
  .line('Port : ' + this.printer.mPort)
  .newline()
  .newline()
  .newline()
  .newline()
  .newline()
  .newline()
  .cut();

const resultByte = result.encode();

// send byte code into the printer
socket.open(
  this.printer.mIp,
  this.printer.mPort,
  () => {
    socket.write(resultByte, () => {
      socket.shutdownWrite();
    });
  },
  (err) => {
    console.error(err);
  }
);
*/  

}

If I comment like this. It can run normally.

ansontio commented 3 years ago

@KApoNG21 someone has the same problem as you, have you declared socket?

https://github.com/NielsLeenheer/EscPosEncoder/issues/29#issuecomment-723554803