J
J
Janserik-Bay2022-04-03 19:41:03
Mongoose
Janserik-Bay, 2022-04-03 19:41:03

How to add an element to an array field?

Hello everyone! I have such a scheme (yes, yes, I'm a govnokoder).

const bookSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    author: {
        type: String,
        require: true,
    },
    description: {
        type: String,
    },
    img: {
        type: String
    },
    genre: {
        type: [String]
    },
    price: {
        type: Number,
        required: true
    },
    rating: {
        type: Number,
        default: 1,
    },
    comments: [
        {
            username: {
                type: String
            },
            comment: {
                type: String,
                minLength: 2,
                maxLength: 1000
            },
            createdAt: {
                type: Date,
                default: Date.now()
            }
        }
    ]
})


Now I need to make it so that I can add a comment to the comments array using this function:
async addComment(req, res) {
       ///:id/comments/:username
        let id = req.params.id
        let name = req.params.name
        let comment = req.body.comment
    }


Prepare to take any advice

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