torbenrudgaard
torbenrudgaard

Reputation: 2551

How to avoid the "[object Object]1" when trying to find.count

When calling this function (right now my order table is empty) I exspect the number 1

let id = 0;
id = await orderNumber();

But the id I get in return is "[object Object]1"

//
// Get MAX order number
//
orderNumber = () => {
    return new Promise((resolve, reject) => {
        return resolve (orderModel.find().count()+1)
    })
}

Anyone have an idea how to avoid that? My purpose here is just to find the MAX number of records in orderModel (order table) and add 1 to that number.

Upvotes: 0

Views: 118

Answers (2)

cbr
cbr

Reputation: 13652

orderModel.find().count() probably returns a Promise, so you need to await for it to get the actual count:

Note: as mentioned by torbenrudgaard, you're better off using .countDocuments()

orderNumber = async () => {
    const count = await orderModel.find().count()
    return count + 1
}

or:

orderNumber = () => {
    return orderModel.find().count().then(count => count + 1)
}

Upvotes: 1

satendra
satendra

Reputation: 113

id = await orderNumber();
orderNumber = () => {
        return new Promise((resolve, reject) => {
                    orderModel.count({}, function(err, result) {
                        if (err) {
                            console.log(err);
                        } else {
                            res.json("Number of documents in the collection: " + result);
                            return resolve({
                                count: result
                            });
                        }
                    });
                }

Upvotes: 1

Related Questions