2

I am trying to create a nested mongoose schema that uses 'type' to create a nested array.

The schema that I think I am having an issue with is "chorePerson".

Here is the data that I am trying to put into a schema:

{
  "chart": [
    {
      "ordinal": 0,
      "chorePerson": [
        {
          "person": "emily",
          "chore": "Catbox"
        },
        {
          "person": "Steve",
          "chore": "Dishes"
        }
      ]
    }
  ]

Here is my current schema. Note the use of "type" for "chart" and "chorePerson"

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const chorePersonSchema = new mongoose.Schema({
    person: {type: String, requried: true},
    chore: {type: String, required: true},
});


const chartSchema = new mongoose.Schema({
    ordinal: {type: Number, required: true},
    chorePerson:{ type: chorePersonSchema },
});


// create the schema
const ChoreChartSchema = new Schema({

    affiliation: {type: String, required: true},
    currentWeekNumber: {type: Number, required: true},
    currentYear: {type: Number, required: true},

    chart:{ type: chartSchema },

    date: {type: Date, default: Date.now},
})

module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)

When I run my code this is what I get before the crash:

{ _id: 5c742ed116a095522c38ddfc,
  affiliation: 'family',
   currentYear: 2019,
   currentWeekNumber: 9,
   date: 2019-02-25T20:26:33.914Z,
  chart: [ { ordinal: 0, chorePerson: [Array] } ] }

I think... chorePerson is causing the error... but I don't know how to fix it. Here is the exception:

(node:6728) UnhandledPromiseRejectionWarning: ObjectExpectedError: Tried to set nested object field `chart` to primitive value `[object Object]` and strict mode is set to throw.

What I have tried

I tried this schema:

const chartSchema = new mongoose.Schema({
    ordinal: {type: Number, required: true},

    chorePerson : [{
        person : String,
        chore : String
        }]       
});

Update:

OK... so I went back to basics and this works, but it's not how I want the final schema to be. Can anybody help out with nesting this ?

// create the schema
const ChoreChartSchema = new Schema({

    affiliation: {type: String, required: true},
    currentWeekNumber: {type: Number, required: true},
    currentYear: {type: Number, required: true},

//    chart:{ type: chartSchema },

    chart:[{
            ordinal: 0,
            chorePerson : [{
                person : String,
                chore : String
            }]
    }],

    date: {type: Date, default: Date.now},
})

1 Answer 1

2

Turns out it was easier than I thought:

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

    const chorePersonSchema = new mongoose.Schema({
        person: {type: String, requried: true},
        personID: {type: String, required: true},
        chore: {type: String, required: true},
        choreID: {type: String, required: true},
    });

    const chartSchema = new mongoose.Schema({
        ordinal: {type: Number, required: true},

        chorePerson : [{ type:chorePersonSchema }]       
    });


    // create the schema
    const ChoreChartSchema = new Schema({

        affiliation: {type: String, required: true},
        weekNumber: {type: Number, required: true},
        year: {type: Number, required: true},

        chart:[{type: chartSchema}],

        date: {type: Date, default: Date.now},
    })

    module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)
Sign up to request clarification or add additional context in comments.

1 Comment

How can you write chart:[{type: chartSchema} OR {type: someOtherSchema}], I mean chart can be array of chartSchema object or someOtherScheme object..is it possible to do a union type?Thanks

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.