hapipal / boilerplate

A friendly, proven starting place for your next hapi plugin or deployment
https://hapipal.com
184 stars 26 forks source link

Add sub-documents from another file (mongoose) #88

Closed littlegraycells closed 4 years ago

littlegraycells commented 4 years ago

I have 2 mongo models defined in 2 different files (generated using hpal make model like so

models/parent.js

'use strict';

const Mongoose = require('mongoose');

module.exports = {
    name: 'parent',
    schema: new Mongoose.Schema({
        name: String,
        age: Number
      })
};

models/child.js

'use strict';

const Mongoose = require('mongoose');

module.exports = {
    name: 'child',
    schema: new Mongoose.Schema({
        name: String,
        age: Number
      })
};

What I'm trying to do is define child as a sub-document array on parent but it throws an error

'use strict';

const Mongoose = require('mongoose');
const Child = require('./child');

module.exports = {
    name: 'parent',
    schema: new Mongoose.Schema({
        name: String,
        age: Number,
        children: [Child]
      })
};

TypeError: Invalid schema configuration: Child is not a valid type at path name. See http://bit.ly/mongoose-schematypes for a list of valid schema types.

Pretty sure something needs to be different in the require statement but not quite sure what it is.