我有一些问题引用了一些动态变量,这些变量等同于基于用户会话位置值的某些mongoose模型。我有两个脚本。
location.js& reporting.js
location.js
module.exports = function(req) {
// Setting some variables to do the following.
// Assign var to value of the client remote address. This should return the ip address.
// The remoteAddress object returns an IPV6 format. Because of this, I setup a new var to the value of the client IP stripped of the ":::ffff" portion.
// I then split the string by the period character to get an array of the sections of string.
var clientIP = req.connection.remoteAddress;
var strippedIP = clientIP.replace(/^.*:/, '');
var splitIP = clientIP.split('.')
// Determining if the site session value based on second octet of ip response.
if (splitIP[1] == '28') {
req.session.site = 'shk';
}
else if (splitIP[1] == '29') {
req.session.site = 'ftm';
}
else if (splitIP[1] == '31') {
req.session.site = 'tpe';
}
else {
req.session.site = 'ftm';
}
// Using case statement to determine the machinery model to use as well as passdowns.
switch(req.session.site) {
// Shakopee Variables
case 'shk':
console.log("You're located in Shakopee.");
var Machinery = require('../models/machinery_shk');
var Loggings = require('../models/passdowns_shk');
break;
// Fort Mill Variables
case 'ftm':
console.log("You're located in Fort Mill.");
var Machinery = require('../models/machinery_ftm');
var Loggings = require('../models/passdowns_ftm');
break;
// Tempe Variables
case 'tpe':
console.log("You're located in Tempe.");
var Machinery = require('../models/machinery_tpe');
var Loggings = require('../models/passdowns_tpe');
break;
// Default values to use if no case is matched.
default:
console.log("You're located in Default");
var Machinery = require('../models/machinery_ftm');
var Loggings = require('../models/passdowns_ftm');
break;
}
};
reporting.js - route
reportingRouter.route('/')
.get((req, res, next) => {
Location(req);
if (req.session.loggedIn === false || req.session.loggedIn === undefined || !req.session.loggedIn) {
res.redirect('/reporting/login')
}
else if (req.session.loggedIn === true) {
Loggings.find({}, (err, loggings) => {
if (err) {
throw err;
}
else {
Machinery.find({}, (err, machinery) => {
if (err) {
throw err;
}
else {
// console.log(machinery)
Shifts.find({}, (err, shifts) => {
if (err) {
throw err;
}
else {
res.render('reporting', { pageTitle: 'Reporting', loggings: loggings, machinery: machinery, shifts: shifts, ldapFullName: req.session.fullName })
// console.log(loggings)
}
})
}
})
}
})
}
})
location.js设置为公开一个带一个参数的函数。该参数是快速"req"
对象。此脚本还采用客户端IP地址。我接受客户端IP并得到字符串的第二个八位字节。
基于该值,我在快速会话中的session
对象的req
对象中分配了一个属性。
req.session.site = <some-value>
设置完成后,我会对分配的值执行切换操作。如果有一些值,请为某些mongoose模型分配更多变量。例如,我为某个猫鼬模型设置了"Loggings"
变量。
var Loggings = require('../models/passdown_<site-id>')
假设分配了这些变量,我应该能够在report.js脚本中"require"
这个脚本。
在reporting.js中,我为该模块分配了一个变量。
var Location = require('location')
然后,我调用该变量并在某些路径上传入req
参数。例如,当在某个路由上执行"GET"
时,我将此Location
模块“函数”称为传递req
对象。
Location(req)
现在,假设所有这些都有效,我是否应该能够执行引用我在location.js中设置的"Loggings"
变量的mongoose查询?我得到了一些undefined
,我相信这是由于范围问题的变化。在这种情况下,我应该"export"
这些模型需要吗?例如,
exports.Loggings = require('../models/passdown_<site-id>')
为此我的无知道歉。
答案 0 :(得分:0)
没关系。我决定将位置检查逻辑放到app.js下的函数中。确定位置变量后,在函数末尾添加next()
方法。这样,在函数运行后,它将进入任何等待的路径。
然后我告诉app使用这个中间件功能。
app.use(checkLocation);
这适合我和应用程序需要。可能不是最好的解决方案,但我让它使用它。如果有人有任何其他意见,我会对此持开放态度。