2017-10-09 2 views
0

사용자 세션 위치 값을 기반으로하는 일부 몽구스 모델과 동일한 몇 가지 동적 변수를 참조하는 데 문제가 있습니다. 두 가지 스크립트가 있습니다.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>') 

, 내 reporting.js 스크립트의 내부 "require"이 스크립트로 할 수 있어야한다.

reporting.js 내부에서 해당 모듈에 변수를 할당합니다.

var Location = require('location') 

그런 다음, 그 변수를 호출하고 일부 노선에 때 req 매개 변수에 전달합니다. 예를 들어, 어떤 경로에서 "GET"이 수행되면이 Location 모듈을 req 객체에 전달되는 "함수"모듈이라고 부릅니다. 이 모든 것이, 내가 location.js에서 설정 한 "Loggings" 변수를 참조하는 몽구스 쿼리를 수행 할 수 없어야 작품 가정 지금

Location(req) 

? undefined이 있는데 변수 범위 문제로 인한 것 같습니다. 이 경우에는 "export" 모델에 필요합니까? 예 :

exports.Loggings = require('../models/passdown_<site-id>') 

여기에 대한 사과드립니다.

답변

0

Nevermind. app.js에서 함수에 위치 확인 로직을 던지기로 결정했습니다. 위치 변수를 결정한 후 함수의 끝에 next() 메서드를 추가했습니다. 이렇게하면, 함수가 실행 된 후에는 기다리는 모든 경로로 진행하게됩니다.

그런 다음이 미들웨어 기능을 사용하도록 앱에 알립니다.

app.use(checkLocation); 

이 기능은 저와 저에게 유용합니다. 아마도 가장 좋은 해결책은 아니지만 이것을 사용하여 작업하게 만들었습니다. 누군가 다른 입력이 있다면, 나는 그것에 대해 개방적이다.