D
D
Db_Al_S2022-02-05 18:15:43
MongoDB
Db_Al_S, 2022-02-05 18:15:43

Counting identical elements in mongodb array. How to count all the items to display the cart?

function corfList(userId,chatId){
    User.findOne({telegramId: userId})
    .then(user => {
        let html
        let amountPr
        let amount

        if(user){
            
            amountPr = {}
            user.corf_list.forEach(a =>{
                amountPr[a] = amountPr[a] + 1 || 1;
            })
            Products.find({db: user.corf_list}).then(product => {
                if(product.length){
                    html =  product.map((p, i) => {
                        for (res in amountPr)
                        amount = 'x' + amountPr[res]
                        return `<b>${i + 1}</b> ${p.name} ` + amount +res
                    }).join('\n')
                }
                const inCorf = 'В корзине:\n'
                sendHTML(chatId, inCorf + html, 'ord_corf')
            })
        } else {
            sendHTML(chatId, `В корзине пока пусто..`, 'home')
            }
    })
}

The code works, the console displays:
{ chk: 4, fre: 1, grk: 1, big: 3 }
But when you open the cart in the bot, it doesn't work quite correctly:
61fe93943d013621501739.jpeg
How to write so that the quantity of the corresponding product is in the cart under the corresponding product?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question