mongo 节点 js 中交叉引用文件的模块导出问题
mongo module export issue with cross referencing files in node js
我有 2 个文件 Stats.js 和 Stock.js。这两个文件都需要访问其他文件,所以我必须在 Stats 中添加 require(Stock) ,反之亦然。但是在 Stats.js 中执行 createstats() 时,它给出 aggregate is not a function in 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);
这是我已经解决的问题。我又添加了一个名为 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);
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);
我有 2 个文件 Stats.js 和 Stock.js。这两个文件都需要访问其他文件,所以我必须在 Stats 中添加 require(Stock) ,反之亦然。但是在 Stats.js 中执行 createstats() 时,它给出 aggregate is not a function in 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);
这是我已经解决的问题。我又添加了一个名为 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);
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);