0

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

2
  • can you do console.log(req.body), what's the output. Commented Apr 2, 2021 at 3:31
  • Its an object that contains the array of objects that i showed first Commented Apr 2, 2021 at 23:56

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.