Skip to content

Instantly share code, notes, and snippets.

@ianpaschal
Created July 9, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianpaschal/23a583cd42c197d15796dc43e3c504d5 to your computer and use it in GitHub Desktop.
Save ianpaschal/23a583cd42c197d15796dc43e3c504d5 to your computer and use it in GitHub Desktop.
Mongoose discriminators don't work?
const mongoose = require( "mongoose" );
const Part = require( "./Part" );
const options = { discriminatorKey: "kind" };
const schema = new mongoose.Schema({
techincal: {
bcd: Number, // mm
bolts: Number,
bbInterface: String, // e.g. "square-taper-jis"
bbLength: Number, // mm
chainline: Number, // mm
crankLenght: Number // mm
}
}, options );
// Export the model
module.exports = Part.discriminator( "CrankRight", schema );
const mongoose = require( "mongoose" );
const options = { discriminatorKey: "kind" };
const schema = new mongoose.Schema({
general: {
brand: String,
group: String,
name: String,
years: [ Number ],
productID: String,
stock: Boolean,
divisible: Boolean,
includes: [ String ]
},
physical: {
colors: [ String ],
finishes: [ String ],
materials: [ String ],
markings: [ String ],
weight: Number,
condition: Number
}
}, options );
// Export the model
module.exports = mongoose.model( "Part", schema );
const express = require( "express" );
const mongoose = require( "mongoose" );
const Part = require( "./models/Part" );
const CrankRight = require( "./models/CrankRight" );
mongoose.connect( "mongodb://localhost:27017/parts" );
const connection = mongoose.connection;
connection.on( "error", console.error.bind( console, "connection error" ) );
connection.once( "open", () => {
console.log( "Connection succeeded" );
});
const app = express();
app.use( require( "morgan" )( "combined" ) );
app.use( require( "body-parser" ).json() );
app.use( require( "cors" )() );
app.listen( process.env.PORT || 8081 );
app.post( "/parts", ( request, response ) => {
/* Example request.body
{
kind: "CrankRight"
general: {
brand: "Shimano",
group: "Dura-Ace",
name: "9100"
},
physical: {
colors: [],
materials: [],
markings: [],
finishes: []
},
technical: {
bcd: 130
}
}
*/
let part;
// Obviously there's a better way to determine which model to use but this works for the gist:
if ( request.body.kind === "CrankRight" ) {
console.log( "Creating a crank right" );
part = new CrankRight( request.body );
} else {
console.log( "Creating a generic part" );
part = new models.Part( request.body );
}
console.log( "PART CREATED IS:", part );
// At this point, part does not contain the technical info, nor does it contain a __t key. It does contain "kind" though.
part.save( ( error ) => {
if ( error ) {
console.log( error );
}
response.send( part );
});
});
@ianpaschal
Copy link
Author

For some reason the technical attributes are not included in the saved document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment