I have an c++ add-on that uses a scanner SDK. When a barcode is scanned, I want to pass that data to my Node.js app. Everything works (including 'connect' event) up until the c++ code tries to emit the 'barcodeScanned' event.
The error I receive is:
#
# Fatal error in v8::HandleScope::CreateHandle()
# Cannot create a handle without a HandleScope
#
Relavent Code:
void NodeZebraAddon::SetEmitFunction(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Function func = info[0].As<Napi::Function>();
EventEmitter = Napi::Persistent(info[0].As<Napi::Function>());
EventEmitter.Call({Napi::String::New(EventEmitter.Env(), "connect")});
}
void NodeZebraAddon::OnBarcodeEvent(short int eventType, std::string &pscanData)
{
Napi::HandleScope scope(EventEmitter.Env());
cout << "Barcode Detected" << endl;
cout << "Out XML" << endl;
cout << pscanData << endl;
EventEmitter.Call({Napi::String::New(EventEmitter.Env(), "barcodeEvent"), Napi::String::New(EventEmitter.Env(), pscanData)});
}
test.js
const emitter = new EventEmitter()
const instance = new NodeZebraAddon();
emitter.on('connect', () => {
console.log('onConnect');
})
emitter.on('barcodeEvent', (evt) => {
console.log(evt);
})
instance.setEmitFunction(emitter.emit.bind(emitter));