我正在构建一个脚本来自动从数据定义文件构建我的mongoose模式。在某一点上,我需要以编程方式将类型定义为另一种类型的数组。
在我的JavaScript代码中,我使用:
PropertySchema.js
import React from 'react';
import SimpleModal from '../common/SimpleModal';
export default class AttachmentModal extends React.Component {
constructor (props) {
super(props);
}
render () {
return (
<SimpleModal showModal={this.props.show}
onToggleModal={this.props.onHide}
title={this.props.title}
onCancelClick={this.props.onHide}
onPrimaryButtonClick={this.props.onPrimaryButtonClick}
cancelText="Cancel"
primaryButtonText={this.props.primaryButtonText}
loading={this.props.loading}
backdrop='static'
bsStyle={this.props.bsStyle}>
{this.props.children}
</SimpleModal>
);
}
}
用于构建模式的javsacript构建器。
schemaBuilder.js
import mongoose from 'mongoose';
const PropertySchema = new mongoose.Schema({
name: {
type: String
},
description: {
type: String
},
value: {
type: mongoose.Schema.Types.Mixed
},
});
export default PropertySchema;
最后import PropertySchema from './PropertySchema';
let schema = {};
schema.name = {
type: "String"
unique: true
required: true
}
schema.props = {
type: [PropertyArray] <=== Error on runtime
}
schema.createdAt = {
type: "Number"
}
fs.writeFile(".", JSON.stringify(schema), "schema.json");
:
MyModel.js
程序运行正常,并生成import schema from './schema.json';
const myschema = new mongoose.Schema(myschema);
export default mongoose.model('MyModel', myschema);
文件。当我在mongoose上运行schema.json
时,我收到以下错误:
MyModel
如何以编程方式定义另一个模式的数组,如下所示,将其保存为JSON文件?
throw new TypeError('Undefined type `' + name + '` at `' + path +
^
TypeError: Undefined type `undefined` at `obj.deleted.required`
Did you try nesting Schemas? You can only nest using refs or arrays.