Skip to content

Commit

Permalink
docs(faq): add faq re: typeKey
Browse files Browse the repository at this point in the history
Fix #1886
  • Loading branch information
vkarpov15 committed Nov 23, 2017
1 parent 48aeccc commit e061881
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/faq.jade
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ block content
console.log(this);
});

hr#type-key
:markdown
**Q**. I have an embedded property named `type` like this:
:js
const holdingSchema = new Schema({
// You might expect `asset` to be an object that has 2 properties,
// but unfortunately `type` is special in mongoose so mongoose
// interprets this schema to mean that `asset` is a string
asset: {
type: String,
ticker: String
}
});
:markdown
But mongoose gives me a CastError telling me that it can't cast an object
to a string when I try to save a `Holding` with an `asset` object. Why
is this?
:js
Holding.create({ asset: { type: 'stock', ticker: 'MDB' } }).catch(error => {
// Cast to String failed for value "{ type: 'stock', ticker: 'MDB' }" at path "asset"
console.error(error);
});
:markdown
**A**. The `type` property is special in mongoose, so when you say
`type: String`, mongoose interprets it as a type declaration. In the
above schema, mongoose thinks `asset` is a string, not an object. Do
this instead:
:js
const holdingSchema = new Schema({
// This is how you tell mongoose you mean `asset` is an object with
// a string property `type`, as opposed to telling mongoose that `asset`
// is a string.
asset: {
type: { type: String },
ticker: String
}
});

hr#date_changes
:markdown
**Q**. Why don't in-place modifications to date objects
Expand Down

0 comments on commit e061881

Please sign in to comment.