当mongoose.model()的collection参数不是硬编码时,如何维护项目结构

时间:2018-11-16 10:51:42

标签: javascript node.js mongodb

一切都很好,但是为了维持项目结构 我需要将mongoose.model('VitalSign',vitalSign,collectionID)移到model.js 问题是collectionID没有硬编码! 我该如何移动?

Router.js

const HTTPStatus = require('http-status');
var getOneHourData = require("../service/VitalService.js");

exports.showData = function(req, res) {
var collectionID =req.params.patientId;
var collectionIDlowercase = collectionID.toLowerCase() ;
getOneHourData(collectionIDlowercase,function(err,context) {
    if (err) {
        res.status(HTTPStatus.BAD_REQUEST).json({ message: "query error" });
        return;
    }
    res.status(HTTPStatus.OK).json(context);
});
};

Model.js

var mongoose = require('mongoose');
var vitalSignsSchema = new mongoose.Schema(
{
    "device_id":{type:String,required:true},
    "data":[{}],
});

service.js

const vitalSign = require("../models/Sign.js");
var  async = require("async");
var mongoose = require('mongoose');
function getModel(model) {
    this.model = model;
}
getModel.prototype = {
    retrieveVitalSign: function(condition, cb) {
        this.model.find(condition, function(err, data) {
            cb(err, data);
        });
    }
}
var nowtime= new Date().getTime();
function getOneHourData(collectionID,callback) {    
    var VitalSign=mongoose.model('VitalSign',vitalSign,collectionID);
    *****// ↑ wanna move this to model.js*****
    var vitalSignModel = new getModel(VitalSign); 
    async.waterfall([
        function(callback){            
            var condition ={time:{$gte : (nowtime-60*60*1000),$lte:nowtime}}
            vitalSignModel.retrieveVitalSign(condition,callback)  ;            
        },
        function(data,callback){
            var recordrrhr ={
                "device_id":data[0].device_id,
            } 
        callback(null,recordrrhr);
        }
    ],function(err,rst){
        if(err) console.log(rst); 
        callback(err,rst);  
    }
    )
}
exports = module.exports = getOneHourData;

0 个答案:

没有答案