0

I have a plugin, cordova-plugin-datecs-printer, and I can print some strings with it on a thermal printer. The problem is that I'm not able to print large numbers of strings this way I'm coding. I have already been informed by the plugin creator that the error is in the way I am dealing with the promises, but I have already tried to change the way I deal with them in different ways and I am not getting it. I tried to look for a few examples and learn more about promises, but I can not apply anything to this case.

My code:

 printItem(){
        this.print.listBluetoothDevices().then(result => {
          console.log(JSON.stringify(result));
          this.print = result;
        }).catch(err => {

        });

        this.print.connect('00:02:5B:B4:7C:3A').then(result => {
          console.log(JSON.stringify(result));
          this.print = result;
        }).catch(err => {

        });

        var printStr = "";
          printStr += "{reset}{center}Instituto do Meio Ambiente {br}";
          printStr += "------------------------------------------------";
          printStr += "AUTO DE INFRACAO AMBIENTAL 9453-D {br}";
          printStr += "------------------------------------------------";
          printStr += "DADOS DO AUTUADO {br}";

        var printStr2 = "";
          printStr2 += " {br}";
          printStr2 += "{reset}{left}Nome: ";
          printStr2 += "ADD VARIAVEL NOME {br}";
          printStr2 += "CPF: ";
          printStr2 += "ADD VARIAVEL CPF {br}";

        //Promises I want to chain
        this.print.printText(printStr, 'ISO-8859-1');
        this.print.printText(printStr2, 'ISO-8859-1');

        this.print.feedPaper(100).then(result => {
          console.log(JSON.stringify(result));
          this.print = result;
        }).catch(err => {

        });

EDIT: Provider I'm using (print):

listBluetoothDevices() {
    return new Promise<any>((resolve, reject) => {
      this.win.DatecsPrinter.listBluetoothDevices((success) => resolve(success), (error) => reject(error));
    });
  }

  connect(deviceAddress: string): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      setTimeout(() => this.win.DatecsPrinter.connect(deviceAddress, (success) => resolve(success), (error) => reject(error)), this.defaultTimeout);
    });
  }

  printText(text: string, charset: string = 'ISO-8859-1'): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      this.win.DatecsPrinter.printText(text, charset, (success) => resolve(success), (error) => reject(error));
    });
  }
leonardofmed
  • 842
  • 3
  • 13
  • 47

3 Answers3

1

All your code is asynchronous, so, all this code will be executed at the same time.

  this.print.connect('00:02:5B:B4:7C:3A').then(result => {
          console.log(JSON.stringify(result));
          this.print = result;
        }).catch(err => {

        });


    var printStr2 = "";
      printStr2 += " {br}";
      printStr2 += "{reset}{left}Nome: ";
      printStr2 += "ADD VARIAVEL NOME {br}";
      printStr2 += "CPF: ";
      printStr2 += "ADD VARIAVEL CPF {br}";

    //Promises I want to chain
    this.print.printText(printStr, 'ISO-8859-1');
    this.print.printText(printStr2, 'ISO-8859-1');

    this.print.feedPaper(100).then(result => {
      console.log(JSON.stringify(result));
      this.print = result;
    }).catch(err => {

    });

To work correctly, you should put all your code inside the first promise, something like this:

this.print.connect('00:02:5B:B4:7C:3A').then(result => {
  console.log(JSON.stringify(result));
  this.print = result;

   // ALL YOUR CODE HERE!!

}).catch(err => {

});
  • Please write your answer in English, since [Stack Overflow is an English site.](//meta.stackexchange.com/q/13676) – Suraj Rao Mar 09 '18 at 14:05
  • 1
    Rewrited the answer. Thank you. – Humberto de Carvalho Mar 09 '18 at 14:19
  • Thanks for the atention! I already tried this way too, but gives me the same problem that i told to @TKoL: Uncaught (in promise) TypeError: _this.print.printText is not a function. I also tried the way that the plugin creator provide in the github page (https://github.com/giorgiofellipe/cordova-plugin-datecs-printer#example) – leonardofmed Mar 09 '18 at 19:07
  • Upvoted to compensate the downvote for not writing in english once you translated your question. – JoeCool Mar 09 '18 at 20:15
0

.then is basically literally what it says it is. Do this, THEN do this

this.print.printText(printStr, 'ISO-8859-1')
    .then(() => this.print.printText(printStr2, 'ISO-8859-1'))
    .then(() => this.print.feedPaper(100));

etc. Obviously the code I provided is from a place of not understanding how your print api is supposed to work, you have to read the documentation to do it correctly.

You should definitely make an effort to become comfortable with promises. Take a minute and read some articles about them.

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • Hi, like I said, I'm no expert in promises, but I already read a lot about it. I also tried to do this way, but only the first string is printed and no error is show. – leonardofmed Mar 09 '18 at 17:24
  • well you have to read the documentation for the api. Nobody here can see what `this.print.printText` is supposed to mean. Maybe you're supposed to do the first printText, then feedPaper(100), then the second printText then feedPaper again, it will tell you this stuff in the documentation. – TKoL Mar 09 '18 at 17:28
  • @leonardofmed I'm not entirely convinced the api even gives you promises. I looked at the code on github and it uses only callbacks. – TKoL Mar 09 '18 at 17:31
  • Thanks for the quick response! Correcting: trying this way give me a error: Uncaught (in promise) TypeError: _this.print.printText is not a function. I also edited the question, added the provider I'm using, now you can see what this.print.printText means. I'm dealing with this as if it were promises because the creator of the plugin said that my problem is how I'm dealing with those promises. (https://github.com/giorgiofellipe/cordova-plugin-datecs-printer/issues/95). Also, I already read the docs and have used some of the examples provided by the plugin creator. – leonardofmed Mar 09 '18 at 19:01
  • if " _this.print.printText is not a function" then you need to figure out where you're calling printText that isn't a function, and fix that. Perhaps you need to do a `that = this` somewhere in the code and start calling `that.print.printText` instead? – TKoL Mar 09 '18 at 19:05
  • I believe that I got the error when I call ".then(() => this.print.printText(printStr2, 'ISO-8859-1'))". I already tried something I founded here: https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback but I don't think I got it right. I will read further about your sugestion. – leonardofmed Mar 09 '18 at 20:11
  • The "that = this" method did not work, returns the same error as before: Uncaugh (in promise): TypeError: that.print.printText is not a function. – leonardofmed Mar 12 '18 at 14:12
  • you have to learn how to debug it yourself. Create a function, set a breakpoint at the place where it's not a function, find out why. – TKoL Mar 12 '18 at 15:40
0

Resolved the issue by adding all texts and variables into a unique string. The problem returns if the number of characters is close or above 7000.

leonardofmed
  • 842
  • 3
  • 13
  • 47