Erraco
Erraco

Reputation: 158

How to save array of objects in mongoose?

Hi so i have an array of objects that look like this

[{
             "id": 0,
             "name": "Del Vecchios | Italian Food | Asheville, NC",
             "domain": "delvecchiositalian.com",
             "email": "[email protected]",
             "phone": "828-258-7222",
            
       },
       {
             "id": 1,
             "name": "DeRango's Pizza Palace | Italian Restaurant | Racine, WI",
             "domain": "derangos.com",
             "email": "[email protected]",
             "phone": "262-639-4112",
             
       },
       {
             "id": 2,
             "name": "Michigan's Premier Flooring Installation Services | D.E. McNabb",
             "domain": "demcnabb.com",
             "email": "no email",
             "phone": "(248) 587-1500",
             
       },
}]

And i want to store it in my mongo database but i dont know how to make the schema, my actual schema looks like this

const mongoose = require("mongoose");

const infoSchema = new mongoose.Schema(
    {
        info: {
            type: String,
            trim: true,
            required: true,
            maxlength: 3200
        }
    },
    { timestamps: true }
);

module.exports = mongoose.model("ScrapedInfo", infoSchema);

and this is the controller for saving the data

router.post('/stored', (req, res) => {
        const info = new ScrapedInfo(req.body)
        info.save((err) => {
                if (err) {
                    console.log(err+"error")
                }
                else{
                        console.log('saved')
                }
            });   
});

Dunno if i am making a mistake in the controller, seems fine to me, but i every time i run the button for the controller, i have the rror ValidationError: info Path info is required

Upvotes: 0

Views: 2552

Answers (2)

William Tuominiemi
William Tuominiemi

Reputation: 29

Your schema should look something like this. The "Path info is required error" is caused by the fact that the key "info" doesn't exist in the Schema.

    const mongoose = require('mongoose')
    const Schema = mongoose.Schema
    
    // Schema
    const infoSchema = new Schema(
        {
            name: {
                type: String,
                required: true,
            },
            domain: {
                type: String,
                required: true
            },
            email: {
                type: String,
                required: true
            },
            phone: {
                type: String,
                required: true
            }
    },
    { timestamps: true }
)

// Model
module.exports = mongoose.model('Info',infoSchema)

If you want to save an array your "info" key should look like this:

info: {
            type: Array,
            trim: true,
            required: true,
            maxlength: 3200
        }

Where the "type" is Array instead of String.

Upvotes: 1

Hormarka Guusha
Hormarka Guusha

Reputation: 21

When trying to update an array of objects, using mongoose, you will have to tell MongoDB that there are pending changes by using the markModified(path) method. after this you will have to call the save() method.

example We have the passwordsArr part of the schema as an array of objects.

// passwordsArr modified

// mark the path as having changes to write to the DB user.markModified('passwordsArr');

// now saves the document user.save(function (err, user) { if (err) return next(err);

Upvotes: 2

Related Questions