从数据库中获取后需要猫鼬单例
Requiring mongoose singleton after fetching from database
我正在尝试使用 mongoose 构建一个单例以将我的应用程序配置存储在数据库中,因此我不是构建模式和模型并使用 module.exports
导出后者,而是获取配置然后导出它,但我得到的只是一个空的 JSON.
这是我的配置模型代码:
var mongoose = require('mongoose');
var configSchema = new mongoose.Schema({
ad: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Ad'
},
max_users: Number
});
var configModel = mongoose.model('Config', configSchema);
configModel.findOne()
.populate('ad')
.exec((error, result) => {
if (!error) {
if (result) {
module.exports = result;
} else {
var default_config = new configModel({
ad: null,
max_users: 100
});
default_config.save();
module.exports = default_config;
}
} else {
throw error;
}
});
在路由中,我只需要文件并在路由中使用它
var config = require('../models/config');
router.get('/', function(req, res, next) {
res.json(config);
}
请注意,动态要求路由范围内的模块没有产生问题。
是不是因为require
无法识别异步任务中导出的变量?有没有正确的方法来处理这个问题?
CommonJS 中的 require
是同步的。尝试定义一个没有特定 module.exports
的文件并在另一个文件中要求它会给你一个 {}
以下可能有效。
var mongoose = require('mongoose');
var configSchema = new mongoose.Schema({
ad: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Ad'
},
max_users: Number
});
var configModel = mongoose.model('Config', configSchema);
module.exports = function (cb) {
configModel.findOne()
.populate('ad')
.exec((error, result) => {
if (!error) {
if (result) {
module.exports = result;
} else {
var default_config = new configModel({
ad: null,
max_users: 100
});
default_config.save();
cb(null, default_config);
module.exports = default_config;
}
} else {
cb(error);
//throw error;
}
});
}
// In your routes
var config = require('../models/config');
router.get('/', function(req, res, next) {
config(function(err, val) {
if (err) {
// send back error
} else {
res.json(val);
}
})
}
我正在尝试使用 mongoose 构建一个单例以将我的应用程序配置存储在数据库中,因此我不是构建模式和模型并使用 module.exports
导出后者,而是获取配置然后导出它,但我得到的只是一个空的 JSON.
这是我的配置模型代码:
var mongoose = require('mongoose');
var configSchema = new mongoose.Schema({
ad: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Ad'
},
max_users: Number
});
var configModel = mongoose.model('Config', configSchema);
configModel.findOne()
.populate('ad')
.exec((error, result) => {
if (!error) {
if (result) {
module.exports = result;
} else {
var default_config = new configModel({
ad: null,
max_users: 100
});
default_config.save();
module.exports = default_config;
}
} else {
throw error;
}
});
在路由中,我只需要文件并在路由中使用它
var config = require('../models/config');
router.get('/', function(req, res, next) {
res.json(config);
}
请注意,动态要求路由范围内的模块没有产生问题。
是不是因为require
无法识别异步任务中导出的变量?有没有正确的方法来处理这个问题?
require
是同步的。尝试定义一个没有特定 module.exports
的文件并在另一个文件中要求它会给你一个 {}
以下可能有效。
var mongoose = require('mongoose');
var configSchema = new mongoose.Schema({
ad: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Ad'
},
max_users: Number
});
var configModel = mongoose.model('Config', configSchema);
module.exports = function (cb) {
configModel.findOne()
.populate('ad')
.exec((error, result) => {
if (!error) {
if (result) {
module.exports = result;
} else {
var default_config = new configModel({
ad: null,
max_users: 100
});
default_config.save();
cb(null, default_config);
module.exports = default_config;
}
} else {
cb(error);
//throw error;
}
});
}
// In your routes
var config = require('../models/config');
router.get('/', function(req, res, next) {
config(function(err, val) {
if (err) {
// send back error
} else {
res.json(val);
}
})
}