0

我有 2 个文件 Stats.js 和 Stock.js。这两个文件都需要访问其他文件,所以我必须在 Stats 中添加 require(Stock) ,反之亦然。但是在 Stats.js 中执行 createstats() 时,它给出的聚合不是 Stock 中的函数。当我发表评论时,问题不存在

** var Stats = require('../Stats'); Stock.js 中的行。**

如何克服这个问题。请帮忙。谢谢

文件 1: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);

文件 2: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);
4

1 回答 1

0

这是我已经解决的问题。我又添加了一个名为 Schemas.js 的文件,如下所示

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,
};

然后更新file1和file2如下

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);

股票.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);
于 2018-07-25T04:45:41.650 回答