astroame
astroame

Reputation: 399

Error: Uncaught (in promise): ReferenceError: Buffer is not defined

I tried using mutable-buffer in my ionic 4 (Angular) project for Bluetooth print. Each time i tried to instantiate the MutableBuffer object as shown below.

I want to use Mutable buffer to create print buffer object as shown in the code below

import {MutableBuffer} from 'mutable-buffer';



@Component({
  selector: 'app-item',
  templateUrl: './item.page.html',
  styleUrls: ['./item.page.scss'],
})
export class ItemPage implements OnInit {

  buff = new MutableBuffer(10, 17);

}

  sendPrintCommandToBluetoothPrinter() {

      this.buff.write('\x1b\x61\x00');
      this.buff.write("Bill Date   ");


      this.buff.write('\x1b\x61\x02');
      this.buff.write("29-07-2019");//to the right
      this.buff.write('\x0a');

       this.buff.write('\x1b\x61\x00');
      this.buff.write("(Inc Tax)");//to the left
       this.buff.write('\x0a');

    this.PrintViaBluetooth(this.buff.buffer);


}

I get the following error:

core.js:15724 ERROR Error: Uncaught (in promise): ReferenceError: Buffer is not defined
ReferenceError: Buffer is not defined
    at new MutableBuffer (mutable-buffer.js:21)
    at new ItemPage (cart.page.ts:24)
    at createClass (core.js:22160)
    at createDirectiveInstance (core.js:22029)
    at createViewNodes (core.js:23255)
    at createRootView (core.js:23169)
    at callWithDebugContext (core.js:24177)
    at Object.debugCreateRootView [as createRootView] (core.js:23687)
    at ComponentFactory_.push../node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create (core.js:21508)
    at ComponentFactoryBoundToModule.push../node_modules/@angular/core/fesm5/core.js.ComponentFactoryBoundToModule.create (core.js:9935)
    at resolvePromise (zone.js:831)
    at zone.js:741
    at rejected (tslib.es6.js:69)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
    at Object.onInvoke (core.js:17299)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:390)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:150)
    at zone.js:889
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:17290)

Upvotes: 2

Views: 1974

Answers (1)

SteamDev
SteamDev

Reputation: 4404

mutable-buffer is a NodeJS library, it utilizes NodeJS's Buffer class freely, assuming your code is executing in a NodeJS runtime.

Ionic is a hybrid framework, your code is executing in a WebView (browser essentially) where Buffer is not natively available.

Try using the Buffer polyfill https://www.npmjs.com/package/buffer#usage

var Buffer = require('buffer/').Buffer  // note: the trailing slash is important!

Upvotes: 1

Related Questions