Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create MongoDB new collection with json inside the schema

I try to create new collection with mongodb.
I wrote the MongoDB shcema, Nodejs+express code and everything work fine until I send parameters with Json inside them.

I was created this schema (and also more code under this schema) but its not work and I got error:

const partySchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: { type: String, required: true },
    datetime: { type: Date, required: true },
    location: {
        scraping_location: { type: String, required: true },
        display_location: { type: String, required: false },
        city: { type: String, required: false },
        adress: { type: String, required: false },
        area: { type: String, required: false }
    },
    picture: { type: String, required: true },
    url: { type: String, required: true },
    affiliate_url: { type: String, required: true },
    music_by: {type: [], required: false },
    music_type: {type: [], required: false },
    min_age_for_men: {type: Number, required: true },
    min_age_for_women: {type: Number, required: true },
    status_check: {type: Boolean, default: false },
    tags: {
        concept: { type: String, required: false },
        free_text: { type: [], required: false }
    },
    producer_id: { type: String, required: false },
    
});

and the code that I wrote with nodejs+express is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const { title, datetime, location, picture, url, affiliate_url, music_by, music_type, min_age_for_men, min_age_for_women, tags, producer_id } = req.body;

const party = new Party({
    _id: new mongoose.Types.ObjectId(),
    title,
    datetime,
    location: { scarping_location: location.location_scarping },
    picture,
    url,
    affiliate_url, 
    music_by,
    music_type,
    min_age_for_men,
    min_age_for_women,
    tags,
    producer_id
});

party.save().then(() => {
    res.status(200).json({
        message: 'Create a new user'
    })
}).catch(error => {
    res.status(500).json({
        error
    })
});

when I send reqeust to the api with this body:

{
    "title": "test",
    "datetime": "2002-07-15T10:00:00+0300",
    "location": {
        "location_scarping": "new york"
    },
    "picture": "test.jpg",
    "url": "https://google.com",
    "affiliate_url": "https://google.com",
    "min_age_for_men": 18,
    "min_age_for_women": 16
}

I got this error:

{
    "error": {
        "errors": {
            "location.scraping_location": {
                "name": "ValidatorError",
                "message": "Path `location.scraping_location` is required.",
                "properties": {
                    "message": "Path `location.scraping_location` is required.",
                    "type": "required",
                    "path": "location.scraping_location"
                },
                "kind": "required",
                "path": "location.scraping_location"
            }
        },
        "_message": "Party validation failed",
        "name": "ValidationError",
        "message": "Party validation failed: location.scraping_location: Path `location.scraping_location` is required."
    }
}

why its happend?

>Solution :

Looks like this is just a typo, from your schema:

scraping_location: { type: String, required: true }

You’re trying to insert: location_scarping not location_scraping

"location_scarping": "new york"

Hopefully that fixes it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading