Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KApoNG21 opened this issue Nov 8, 2020 · 3 comments
Open

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

KApoNG21 opened this issue Nov 8, 2020 · 3 comments

Comments

@KApoNG21
Copy link

KApoNG21 commented Nov 8, 2020

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
Copy link
Owner

ansontio commented Nov 8, 2020

@KApoNG21
Copy link
Author

KApoNG21 commented Nov 8, 2020

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
Copy link
Owner

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants