I have defined a Mongose schema that has an array of another schema, as follows:
The Property schema/model:
import mongoose from 'mongoose';
const PropertySchema = new mongoose.Schema({
name: {
type: String
},
description: {
type: String
},
value: {
type: mongoose.Schema.Types.Mixed
},
unit: {
type: String
},
});
export default mongoose.model('Property', PropertySchema);
The Customer schema/model:
import mongoose from 'mongoose';
import { PropertySchema } from './Property';
const CustomerSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true
},
description: {
type: String
},
properties: {
type: [PropertySchema]
},
deleted: {
type: Boolean
},
});
CustomerSchema.statics.createTestInstance = function(name, description, properties, callback) {
let personnel = new this ({
name: name,
description: description,
properties: properties,
deleted: false
});
personnel.save(callback);
}
export default mongoose.model('Customer', CustomerSchema);
Generating test data:
function getRandomProperty() {
let rand = Math.floor(Math.random() * 1000);
let type = Math.floor(Math.random() * 4);
let typeArray = [ 124, 2.345, 'Test', true];
let unitArray = [ null, 'inches', 'certification', 'existance']
let prop = {};
prop.name = 'Random Property ' + rand;
prop.description = 'Description Property ' + rand;
prop.value = typeArray[type];
prop.unit = unitArray[type];
return prop;
}
function generateTestCustomer(name, description) {
let props = [];
for (let i = 0; i < 10; i++)
props.push(getRandomProperty());
CustomerModel.createTestInstance(name, description, props, function (err) {
if (err)
throw new Error('ERROR => Error creating test Customer: ' + err);
console.log('LOG => Test Customer created: ' + name);
});
}
And the result:
Error: ERROR => Error creating test Customer: ValidationError: properties: Cast to Array failed for value "[ { name: 'Random Property 408',
description: 'Description Property 408',
value: 124,
unit: null },
{ name: 'Random Property 585',
description: 'Description Property 585',
value: 124,
unit: null },
{ name: 'Random Property 277',
description: 'Description Property 277',
value: true,
unit: 'existance' },
{ name: 'Random Property 709',
description: 'Description Property 709',
value: 2.345,
unit: 'inches' },
{ name: 'Random Property 718',
description: 'Description Property 718',
value: 2.345,
unit: 'inches' },
{ name: 'Random Property 836',
description: 'Description Property 836',
value: true,
unit: 'existance' },
{ name: 'Random Property 944',
description: 'Description Property 944',
value: 2.345,
unit: 'inches' },
{ name: 'Random Property 622',
description: 'Description Property 622',
value: true,
unit: 'existance' },
{ name: 'Random Property 757',
description: 'Description Property 757',
value: 'Test',
unit: 'certification' },
{ name: 'Random Property 508',
description: 'Description Property 508',
value: 124,
unit: null } ]" at path "properties"
at D:/DEV/v9/test/data/generatedata.js:72:10
at D:\DEV\v9\node_modules\mongoose\lib\model.js:3800:16
at D:\DEV\v9\node_modules\mongoose\lib\services\model\applyHooks.js:155:17
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
at Function.Module.runMain (module.js:606:11)
at Object.<anonymous> (D:\DEV\v9\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
The generated array for Property seems to be fine, but I can´t find out why mongoose is throwing this error.
You have imported your property schema to your customer model:
import { PropertySchema } from './Property';
...but you have not exported the property schema.
You have only exported the property model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.