Skip to content

Error: [ERR_STREAM_DESTROYED] Cannot call write after a stream was destroyed. #91

@KylianJay

Description

@KylianJay

`Hi! I'm getting an error that seemingly came out of nowhere, as it didn't happen during my testing phase, but did happen during my production phase.

When trying to print a receipt using my below code:
`app.post('/create/order', async (req, res) => {
// we're getting an order! let's print it!
console.log("printing order with id: #" + req.body.orderId)
console.log(req.body)
let foodString = "";
let drinksString = "";
let dessertsString = "";

if (req.body.items && Array.isArray(req.body.items) && req.body.items.length > 0) {
    const donItems = req.body.items[0].don;
    const drinkItems = req.body.items[0].drinks;
    const dessertsItems = req.body.items[0].desserts;

    if (donItems && Array.isArray(donItems)) {
        const food = donItems.map(donItem => {
            let itemString = donItem.name.replace(/[^\P{L}a-zA-Z]/g, '') + ` `;

            // Check if extraToppings exist and is an array before adding the line
            if (Array.isArray(donItem.selectedToppings) && donItem.selectedToppings.length > 0) {
                const options = donItem.selectedToppings.map(topping => topping.name.replace(/[^\P{L}a-zA-Z\s]/g, ''));
                itemString += `\n Options: (${options.join(', ')})`;
            }

            return itemString;
        });
        foodString = food.join("\n ");
    }

    if (drinkItems && Array.isArray(drinkItems)) {
        const drinks = drinkItems.map(drinkItem => {
            let itemString = drinkItem.name.replace(/[^\P{L}a-zA-Z]/g, '') + ``;

            // Check if milkOptions exist before adding the line
            // Check if extraToppings exist and is an array before adding the line
            if (Array.isArray(drinkItem.selectedToppings) && drinkItem.selectedToppings.length > 0) {
                const options = drinkItem.selectedToppings.map(topping => topping.name.replace(/[^\P{L}a-zA-Z\s]/g, ''));
                itemString += `\n Options: (${options.join(', ')})`;
            }

            return itemString;
        });
        drinksString = drinks.join("\n ");
    }

    if (dessertsItems && Array.isArray(dessertsItems)) {
        const desserts = dessertsItems.map(dessertItem => {
            let itemString = dessertItem.name.replace(/[^\P{L}a-zA-Z]/g, '') + ``;

            // Check if extraToppings exist and is an array before adding the line
            if (Array.isArray(dessertItem.selectedToppings) && dessertItem.selectedToppings.length > 0) {
                const options = dessertItem.selectedToppings.map(topping => topping.name.replace(/[^\P{L}a-zA-Z\s]/g, ''));
                itemString += `\n Options: (${options.join(', ')})`;
            }

            return itemString;
        });
        dessertsString = desserts.join("\n ");
    }

    const separationLine = "-------------------------";

    // Check if there are items for Food before adding the separation line
    if (foodString && (drinksString || dessertsString)) {
        foodString += `\n${separationLine}\n`;
    }

    // Check if there are items for Drinks before adding the separation line
    if (drinksString && dessertsString) {
        drinksString += `\n${separationLine}\n`;
    }
}

try {
    const separationLine = "-------------------------";
    console.log("sending " 
    + "Order ID: " + req.body.id
    + "Table: " + req.body.table
    + "Food: " + foodString
    + "Drinks: " + drinksString
    + "Desserts: " + dessertsString
    + "Total: " + req.body.priceTotal
    + "Date: " + req.body.datetime
    + "Notes: " + req.body.notes
    + "to printer")
     device.open(async function(error){
        await printer
        .font('b')
        .align('ct')
        .size(1,1)
        .text("Receipt 1 of 2")
        .style('bu')
        .size(2, 2)
        .text('New order for Table: ' + req.body.table)
        .text(`\n${separationLine}\n`)
        .text('Food: ' + foodString)
        .text('Drinks: ' + drinksString)
        .text('Desserts: ' + dessertsString)
        .text(`\n${separationLine}\n`)
        .text('Notes: ' + req.body.notes)
        .font('a')
        .size(1, 1)
        .text(`\n${separationLine}\n`)
        .text('Total: ' +  req.body.priceTotal)
        .text('Date: ' + req.body.datetime)
        .text('Order ID: ' + req.body.id)
        .text(" ")
        .cut()
        .font('b')
        .align('ct')
        .size(1,1)
        .text("Receipt 2 of 2")
        .style('bu')
        .size(2, 2)
        .text('New order for Table: ' + req.body.table)
        .text(`\n${separationLine}\n`)
        .text('Food: ' + foodString)
        .text('Drinks: ' + drinksString)
        .text('Desserts: ' + dessertsString)
        .text(`\n${separationLine}\n`)
        .text('Notes: ' + req.body.notes)
        .font('a')
        .size(1, 1)
        .text(`\n${separationLine}\n`)
        .text('Total: ' +  req.body.priceTotal)
        .text('Date: ' + req.body.datetime)
        .text('Order ID: ' + req.body.id)
        .text(" ")
        .cut()
        .close()
        if (error) {
            console.log(error)
        }
    })
}
catch (error) {
    console.log(error)
}

res.send("Order: " + req.body.id + " Printed on date: " + req.body.datetime)

})I get the following error:Error [ERR_STREAM_DESTROYED] Cannot call write after a stream was destroyed`
See pictures below for full error:
IMG_5667
IMG_5668

Is there an issue with my code, is it my Node version? (v20.11.0) I can't seem to get it working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions