title: How to import one mongoose schema to another?
body: <p>I'm making a node app to consume json API and I'd like to separate parts of <code>User</code> schema into separate files because there are many fields in <code>Profile</code> and separating files keeps things cleaner:</p>
  
  <p>So basically instead of </p>
  
  <pre><code>const userSchema = new Schema({
      username: { type: String, required: true },
      password: { type: String, required: true }, 
      profile: { 
        gender: {
         type: String,
         required: true
         },
        age: {
          type: Number
        },
        //many more profile fields come here
  
      }
  });
  </code></pre>
  
  <p>I do this:</p>
  
  <p><code>models/Profile.js</code> is:</p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  
  const profileSchema = new Schema({
        gender: {
        type: String,
        required: true
        },
        age: {
          type: Number
        }
        //the rest of profile fields
  });
  
  module.exports = Profile = mongoose.model('profile', profileSchema);
  </code></pre>
  
  <p>And the <code>models/User.js</code> is:</p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  const Profile = require('./Profile');
  
  const userSchema = new Schema({
      username: { type: String, required: true },
      password: { type: String, required: true }, 
      profile: {type: Schema.Types.ObjectId, ref: 'Profile'},
  });
  
  module.exports = mongoose.model('users', userSchema);
  </code></pre>
  
  <p>The data for <code>User</code> and <code>Profile</code> are posted in the same json post. </p>
  
  <p>However when node tries to save the object I get this error:</p>
  
  <pre><code>(node:4176) UnhandledPromiseRejectionWarning: ValidationError: users validation failed: profile: Cast to ObjectID failed for value "{ gender: 'male'...
  </code></pre>
  
  <p>How can I fix this?</p>