E
E
error5022020-05-29 22:51:45
Node.js
error502, 2020-05-29 22:51:45

What does the ._doc property mean in mongoose?

Here I want to send an array of orders objects to render. When "o"
has the "_doc" property, the code works, but I remove it and the object is lost.

res.render(`orders`, {
            isOrders: true,
            orders: orders.map(o => {
                return {
                    ...o._doc,
                    price: o.courses.reduce((total, c) => {
                        return total += c.count * c.course.price
                    }, 0)
                }
            })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2020-05-30
@error502

with models it is necessary to work through api of these models.
To get data as a simple object, there is a toJSON method.
_doc should not be touched or used - this is internal data storage. Tomorrow update the lib and it may just disappear, or change as you like.

A
Alexander Cheremkhin, 2020-05-30
@Che603000

res.render(`orders`, {
            isOrders: true,
            orders: orders.map(o => {
                return {
                    ...o.toJSON(), // !! используйте вместо _doc
                    price: o.courses.reduce((total, c) => {
                        return total += c.count * c.course.price
                    }, 0)
                }
            })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question