【问题标题】:Pass a hardcoded value through mongoose schema通过猫鼬模式传递硬编码值
【发布时间】:2019-01-23 01:25:37
【问题描述】:

我想每次都发布一些硬编码值以及用户输入(变量)。
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]  
});

原问题Asynchronous Programming in node js to pass constants/predefined mandatory values through mongoose model.

我有点进退两难,比如我应该在 router.post() 方法中传递这个硬编码的值(如果可能的话,我应该如何编码?)还是在模式中?请指导我正确的方向。

【问题讨论】:

  • 你应该修改router.post里面的变量来包含你想要的值
  • @Khang 你是对的我也这么认为,但是我想传递的数据必须是这样的: { filename: ,name, location: , -i {name: , order]...}
  • 我不明白,你遇到了什么问题?
  • github.com/darshanan24/Farhan 这是我的 git hub 存储库,请您查看代码。你会得到一个概述。 @Khang

标签: node.js express mongoose mongoose-schema


【解决方案1】:

如果我误解了这个问题,请原谅。

由于您使用的是 mongoose 架构,因此您可以将字段 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 
    });
)

如果架构在正确的上下文中,我假设您可以轻松地将这些字符串替换为传递的值或交换默认函数。

【讨论】:

  • 嗨,@akrion,你完全理解我的问题,但我应该发布的实际数据是 docs.google.com/document/d/…。现在,当我传递 args 数据时,我在代码中进行了硬编码,这给了我投射错误。 ValidationError: EnrichedEvent 验证失败: livy: Cast to Array failed for value
猜你喜欢
  • 2018-06-11
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2015-02-24
  • 2016-08-05
  • 2014-02-21
  • 2013-02-04
  • 2020-08-25
相关资源
最近更新 更多