Diving deeper into D. Lowe's response that initially helped me but required some adjustments. (I wanted to leave this as a comment, but I lack the necessary reputation points. Plus, I want to ensure I can refer back to this information in a few months when faced with a similar issue and forget how to resolve it.)
If you're bringing in a Schema from another file, make sure to include .schema at the end of the import statement.
Note: While unsure whether using local schemas instead of importing them could trigger an Invalid schema configuration error, personally, I find importing cleaner and more manageable.
For instance:
// ./models/other.js
const mongoose = require('mongoose')
const otherSchema = new mongoose.Schema({
content:String,
})
module.exports = mongoose.model('Other', otherSchema)
//*******************SEPARATE FILES*************************//
// ./models/master.js
const mongoose = require('mongoose')
//If you omit .schema at the end, you'll encounter the "Invalid schema configuration: `model` is not a valid type" error
const Other=require('./other').schema
const masterSchema = new mongoose.Schema({
others:[Other],
singleOther:Other,
otherInObjectArray:[{
count:Number,
other:Other,
}],
})
module.exports = mongoose.model('Master', masterSchema);
You can then easily assign 'other' to 'master' whenever needed (e.g., in my Node.js API).
For example:
const Master= require('../models/master')
const Other=require('../models/other')
router.get('/generate-new-master', async (req, res)=>{
//load all others
const others=await Other.find()
//generate a new master from your others
const master=new Master({
others,
singleOther:others[0],
otherInObjectArray:[
{
count:1,
other:others[1],
},
{
count:5,
other:others[5],
},
],
})
await master.save()
res.json(master)
})