在节点js中使用if条件发布数据所需的逻辑(需要验证)

时间:2018-07-18 07:23:33

标签: javascript node.js mongodb mongoose mongoose-schema

这是我现有的架构,

function! MyHighlights() abort
    highlight Normal      ctermbg=NONE
    highlight NonText     ctermbg=NONE
    highlight EndOfBuffer ctermbg=NONE
endfunction

augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
augroup END
colorscheme foobar

我的要求是 所有EnrichedEvents应该至少有2个列,其中

类型= ID,     类型= TIME。

CSS : 

my-custom-element {
   background-color : red;
}

我所能想到的就是使用if条件,诸如此类。

const mongoose = require('mongoose');

const position = mongoose.Schema({
    startIndex: { type: Number, required: true },
    endIndex: { type: Number, required: false },
}); 

const Column = mongoose.Schema({
    name: { type: String, required: true },
    type: { type: String, enum: ["Description","ID","Time"], required: true },
    positions: [position],
    dataType: { type: String, enum: ["int", "float", "Double","String"], required: true },
    oldName: {type:String, required: false }
});

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 },
    DataSourceID: { type: String, required: false},
    delimiter: { type: String, required: true },
    format: {type:String, enum:["JSON","DELIMITED","FixedWidth","LOG"], required: true},
    columns:[Column]
});

module.exports = mongoose.model('EnrichedEvent', enrichedEventSchema);


Api for Post route:

router.post("/:projectId/events/enriched", (req, res, next) => {
    const enrichedEvent = new EnrichedEvent({
        _id: mongoose.Types.ObjectId(),
        name: req.body.name,
        projectId: req.params.projectId, //taking from url
        delimiter: req.body.delimiter,
        format: req.body.format,
        columns:req.body.columns,
     });

    return enrichedEvent.save()
        .then(result => {
            res.status(201).json({
                message: "Event stored",
                createdEvent: {
                    _id: result._id,
                    projectId: result.projectId,
                    name: result.name,
                    type: result.type,
                    delimiter: result.delimiter,
                    columns:result.columns,
                    format:result.format
                }
            });
        })
        .catch(err => {
            console.log(err);
            res.status(500).json({
                error: err
            });
        });
});

我在这里呆了3天,没有逻辑。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试制定两种不同的方案。

const positionDel = mongoose.Schema({
    startIndex: { type: Number, required: true },
    endIndex: { type: Number, required: false },
}); 
const positionFixed = mongoose.Schema({
    startIndex: { type: Number, required: true },
    endIndex: { type: Number, required: true},
});

答案 1 :(得分:0)

router.post("/:projectId/events/enriched", (req, res, next) => {
    const enrichedEvent = new EnrichedEvent({
        _id: mongoose.Types.ObjectId(),
        name: req.body.name,
        projectId: req.params.projectId, //taking from url
        delimiter: req.body.delimiter,
        format: req.body.format,
        columns:req.body.columns,
     });

     if(delimiter=='DELIMITED'){
        return enrichedEvent_SchemaForDELIMITED.save()
        .then(result=>{
            //use the result.
        })
        .catch(err=>{
            // error thrown
        })
     }
     else if(delimiter=="FIXEDWIDTH"){
        return enrichedEvent_SchemaForFIXED.save()
        .then(result=>{
            //use the result.
        })
        .catch(err=>{
            // error thrown
        })
     }

});

查看代码。逻辑将是这样。