Type Map doesn't support a schema as a value
Closed this issue · 5 comments
beastmike1 commented
As is it defined in https://mongoosejs.com/docs/schematypes.html you can set up a schema as a value for type Map
In the Mongoose Example:
const userSchema = new Schema({
socialMediaHandles: {
type: Map,
of: new Schema({
handle: String,
oauth: {
type: ObjectId,
ref: 'OAuth'
}
})
}
});
const User = mongoose.model('User', userSchema);
but if you do it it fails to get the name in
Line 14 in 18989e0
with an error TypeError: Cannot read property 'toLowerCase' of undefined
faboulaws commented
Fixed. Please validate and close the issue. Thanks.
beastmike1 commented
Hi, I have been testing and It succed to create a mock object based in a schema but it's failing to set up static fields
describe('generate(staticFields)', () => {
const embed = new Schema({ name: String });
const thingSchemaDef = {
str: { type: String },
nested: { name: String },
doubleNested: {
nested: { name: String },
},
embedded: embed,
doubleEmbed: {
nested: embed,
},
ofString: [String],
ofObject: [{ name: String }],
ofEmbedded: [embed],
};
const staticFields = {
str: 'hello',
nested: { name: 'nested' },
doubleNested: {
nested: { name: 'doubleNested' },
},
embedded: { name: 'embedded' },
doubleEmbed: {
nested: { name: 'doubleEmbed' },
},
ofString: ['ofString', 'ofSTring'],
ofObject: [{ name: 'ofObject' }, { name: 'ofObject' }],
ofEmbedded: [{ name: 'ofEmbedded' }, { name: 'ofEmbedded' }],
};
if (mongoose.Types.Map) {
Object.assign(thingSchemaDef, {
map: Map,
mapOfString: {
type: Map,
of: String,
},
mapOfSchema: {
type: Map,
of: new Schema({
name: String,
}),
},
});
Object.assign(staticFields, {
mapOfString: { key: 'mapOfString' },
mapOfSchema: { key: { name: 'mapOfSchema' } },
});
}
const thingShema = new Schema(thingSchemaDef);
it('should use static value', () => {
const thingMocker = mocker(thingShema);
const mock = thingMocker.generate(staticFields);
// expect(mock).to.deep.include(staticFields);
const paths = ['str', 'nested.name', 'doubleNested.nested.name', 'embedded.name', 'doubleEmbed.nested.name', 'ofString', 'ofObject', 'ofEmbedded'];
if (mongoose.Types.Map) {
paths.push('mapOfString', 'mapOfSchema');
}
paths.forEach((path) => {
expect(get(mock, path)).to.eql(get(staticFields, path));
});
});
});
faboulaws commented
Good catch. I'll take a look soon.
faboulaws commented
Fixed. Close after validating that it works :-).
beastmike1 commented
It works like a charm, thanks! 😄