【发布时间】:2018-01-31 09:54:41
【问题描述】:
我已经定义了一个 Mongose 模式,其中包含另一个模式的数组,如下所示:
属性架构/模型:
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);
客户架构/模型:
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);
生成测试数据:
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);
});
}
结果:
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
Property 生成的数组似乎没问题,但我不知道为什么 mongoose 会抛出这个错误。
【问题讨论】:
-
尝试将架构字段定义为
properties: [propertySchema] -
问题依旧。没有区别。
-
let prop = new propertySchema()
标签: javascript arrays mongodb mongoose