我已经定义了一个具有另一个模式数组的Mongose模式,如下所示:
属性架构/模型:
.element {
background-color: rgba(0,0,0,.1);
background-image: url('images/filter/grunge.png');
}
客户架构/模型:
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);
});
}
为Property生成的数组似乎很好,但是我不知道为什么mongoose会抛出这个错误。
答案 0 :(得分:0)
您已将属性架构导入客户模型:
import { PropertySchema } from './Property';
...但您尚未导出属性架构。
您只导出了属性模型。