Search code examples
mongoosemodule-export

mongo module export issue with cross referencing files in node js


I have 2 files Stats.js and Stock.js. Both files need access to other file , so I have to add require(Stock) in Stats and vice versa. But while executing createstats() in Stats.js , it gives aggregate is not a function in Stock. Issue is not there when I comment

** var Stats = require('../Stats'); line in Stock.js. **

How to overcome this issue. pl help. Thanks

File1: Stats.js

var mongoose     = require('mongoose');
var Stock    = require('../Stock');
var logger   = require('../../util/log');
var Schema  = mongoose.Schema;
var StatsSchema   = new Schema({
    item:String,
    pqty:Number,
    sqty:Number,
    date:Date,
});


function createstats () {

    Stock.aggregate([

    .....
    .....
    ])
}
module.exports = mongoose.model('Stats', StatsSchema);

FIle2: Stock.js

    var mongoose     = require('mongoose');    
    var Stats    = require('../Stats');
    var logger   = require('../../util/log');
    var Schema       = mongoose.Schema;    

    var StockSchema   = new Schema({
     supplier:String,
     name:Number,
     rate:Number,
     date:Date,   
    });

    function updateqty () {
        Stats.update(

        .....
        .....
        )
    }
    module.exports = mongoose.model('Stock', StockSchema);

Solution

  • This is the I have solved the problem. I have added one more file called Schemas.js as below

    Schemas.js

     var mongoose     = require('mongoose');    
        var Stats    = require('../Stats');
        var logger   = require('../../util/log');
        var Schema       = mongoose.Schema;    
    
        var StockSchema   = new Schema({
         supplier:String,
         name:Number,
         rate:Number,
         date:Date,   
        });
    
    var StatsSchema   = new Schema({
        item:String,
        pqty:Number,
        sqty:Number,
        date:Date,
    });
    
    
    
    module.exports = {
        StatsSchema:StatsSchema,
        StockSchema:StockSchema,
    };
    

    then updated file1 and file2 as below

    stats.js

     var mongoose     = require('mongoose');
        var logger   = require('../../util/log');
    
        var modelSchemas = require('../Schemas');
        var StatsSchema=modelSchemas.StatsSchema;
        var StockSchema=modelSchemas.StockSchema;
        Stock = mongoose.model('Stock', StockSchema);
        Stats = mongoose.model('Stats', StatsSchema);
    
    
        function createstats () {
    
            Stock.aggregate([
    
            .....
            .....
            ])
        }
        module.exports = mongoose.model('Stats', StatsSchema);
    

    stock.js

    var mongoose     = require('mongoose');    
        var logger   = require('../../util/log');
    
    var modelSchemas = require('../Schemas');
    var StatsSchema=modelSchemas.StatsSchema;
    var StockSchema=modelSchemas.StockSchema;
    
    Stock = mongoose.model('Stock', StockSchema);
    Stats = mongoose.model('Stats', StatsSchema);
    
        function updateqty () {
            Stats.update(
    
            .....
            .....
            )
        }
        module.exports = mongoose.model('Stock', StockSchema);