我有一些问题引用了一些动态变量,这些动态变量等同于基于用户会话位置值的某个猫鼬模型。我有两个脚本。NodeJS引用导出模块
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 - 航线
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并获取字符串的第二个八位字节。
基于该值,我在快速会话中为req
对象的session
对象分配属性。
req.session.site = <some-value>
一旦设置好了,我就对该值赋值执行开关情况。如果有某个值,则将更多变量分配给某些猫鼬模型。例如,我为某个猫鼬模型设置了一个"Loggings"
变量。
var Loggings = require('../models/passdown_<site-id>')
假设这些变量分配,我应该能够"require"
这个剧本我reporting.js脚本里面。
在reporting.js中,我为该模块分配了一个变量。
var Location = require('location')
然后,我称之为变量,并通过在req
参数时的一些路线。例如,当在某个路由上执行"GET"
时,我将这个Location
模块的“功能”称为req
对象。
Location(req)
现在,假设所有的这个作品,我不应该能够执行一个猫鼬查询参照的"Loggings"
可变我location.js设置?我收到了一些undefined
,我认为这是由于可变范围问题。在这种情况下,我应该"export"
这些模型需要?例如,
exports.Loggings = require('../models/passdown_<site-id>')
道歉我的无知在这里。