Open KylianJay opened 4 months ago
The error "Can't close device with a pending request" typically occurs when trying to close the USB device connection while there are still ongoing or pending I/O operations. This means that your application is attempting to close the connection before all data transfers have finished. To resolve this, you need to ensure that all pending operations are completed or canceled before calling the close
method on the device. This might involve adding logic to track ongoing operations and waiting for these to complete before proceeding with the device closure.
Here's a more structured approach to manage printing operations asynchronously, which can help prevent such errors:
const print = (text) => {
return new Promise((resolve, reject) => {
device.open(function (error) {
if (error) {
return reject(error);
}
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text(text)
.cut()
.close(() => {
resolve();
});
});
});
};
By wrapping your printing logic in a Promise and using async/await, you can ensure that the device is not closed until all operations have been completed. This approach helps in managing the lifecycle of the stream more effectively and reduces the likelihood of encountering errors related to premature closure of the device.
Additionally, ensure that all libraries, especially escpos
and its dependencies, are up to date and compatible with your Node.js version (v20.11.0). Incompatibilities can also lead to unexpected errors, including those related to stream and device management.
To continue the conversation, mention @dosu.
I've switched to using USB adapter since I thought this would fix my many stability issues. It did not. Instead, I now get this error. What is causing this? `/home/kylianjay/Documents/osozai/osozai-qrmenu-master/pi/node_modules/usb/dist/usb/device.js:80 this.__close(); ^
Error: Can't close device with a pending request at Device.close (/home/kylianjay/Documents/osozai/osozai-qrmenu-master/pi/node_modules/usb/dist/usb/device.js:80:14) at USBAdapter.close (file:///home/kylianjay/Documents/osozai/osozai-qrmenu-master/pi/node_modules/@node-escpos/usb-adapter/dist/index.mjs:133:20) at file:///home/kylianjay/Documents/osozai/osozai-qrmenu-master/pi/node_modules/@node-escpos/core/dist/index.mjs:1159:20 at new Promise ()
at Printer.close (file:///home/kylianjay/Documents/osozai/osozai-qrmenu-master/pi/node_modules/@node-escpos/core/dist/index.mjs:1158:12)
Node.js v20.11.0 ` I'm on a Raspberry Pi Model 3, for context.
The code in question that causes this issue is here: `app.post('/create/order', async (req, res) => { // we're getting an order! let's print it! console.log("printing order with id: #" + req.body.id) console.log(req.body) let foodString = ""; let drinksString = ""; let dessertsString = "";
})`
Again, any help is greatly appreciated!