我想每次都发布一些硬编码的值以及用户输入(变量)。
args: [{ type: mongoose.Schema.Types.Mixed, required: true }]
>>在此数组中,我想传递一些硬编码值以及用户输入变量。
我要发布的数据看起来像这样。
{"file": "**<user input>**","name":"<user input>", "className": "com.izac.Parser.IO", "args": ["-i", "{\"enrichedKafkaOptions\": {\"checkpointLocation\": \"**<hard coded path always remains the same>**", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoEnriched\"}, \"rejectedKafkaOptions\": {\"checkpointLocation\": \"/Users/vipulrajan/Desktop/checkpoints/DemoRejected\", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoRejected\"} }","-s", "{\"master\":\"local[*]\", \"appName\":\"app1\", \"config\":{\"jvm.memory\":\"4g\"} }"]};
这是我的模式,
const mongoose = require('mongoose');
const livy_schema = mongoose.Schema({
file: { type: String, required: true },
name: { type: String, required: true },
className: { type: String, required: true },
args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to
});
const kafka_schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true, unique: false },
config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to
});
const enrichedEventSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
name: { type: String, required: true, unique: true },
description: { type: String, required: false },
type: { type: String, enum: ["Enriched"], required: true },
format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
kafka: [kafka_schema],
livy: [livy_schema]
});
我有点陷入困境,就像我应该在router.post()方法(如果可能,我应该如何编码?)或模式中传递此硬编码值一样吗?请指引我正确的方向。
答案 0 :(得分:0)
如果我对这个问题有误解,请原谅。
由于您使用的是猫鼬模式,因此您可以将字段default
用作可以初始化和添加hardcoded
值的函数。
类似这样的东西:
const livy_schema = mongoose.Schema({
file: {
type: String,
required: true
},
name: {
type: String,
required: true
},
className: {
type: String,
required: true
},
args: [{
type: mongoose.Schema.Types.Mixed,
required: true,
default: function() {
return { data: 'hardcoded!', info: 'hardcoded!' }
}
}] //here i have constants to pass on to
});
)
如果架构在正确的上下文中,我认为您可以轻松地将这些字符串替换为要传递的值或交换默认函数。