0

How to get only certain values ​​from an array?

module.exports.getAll = async function (req, res) {
    try {
        const cards = await Card.find({})
        res.status(200).json(cards)
    } catch (e) {
        errorHandler(res, e)
    }
}

I am currently displaying an array in this form:

[
    {
        "ncard": "123123",
        "name": "sdf",
        "lim": "0",
        "ost": "0",
        "message": " ",
        "status": "1",
        "_id": "5f030111f5df83755eb65721",
        "date": "2020-07-06T10:46:41.001Z",
        "__v": 0
    },
...
]

But I want to receive in this form:

   [
        {
            "ncard": "123123",
            "name": "sdf",
            "_id": "5f030111f5df83755eb65721",
            "date": "2020-07-06T10:46:41.001Z",
            "__v": 0
        },
    ...
    ]

How can I do that?

1

2 Answers 2

1

try this line

  module.exports.getAll = async function (req, res) {
        try {
            const cards = await Card.find({}).project({ ncard: 1, name: 1, date: 1})
            res.status(200).json(cards)
        } catch (e) {
            errorHandler(res, e)
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

-1

you can try this.
await Card.find({}).project({ ncard: 1, name: 1, date: 1})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.