Last active
August 29, 2015 14:17
-
-
Save krlicmuhamed/c07bbf08cde4bd866082 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var pg = require('pg'); | |
var db = 'notconn'; | |
exports = module.exports = function(settings) { | |
db = new pg.Client(settings.database.connection.string); | |
db.connect(function(err, db, done) { | |
if(err) { | |
/* | |
server.log('error', 'Database connection ' + err); | |
Static.ShutdownProcedure(); | |
*/ | |
console.log(configurations); | |
console.log('Database connection ' + err); | |
db = 'notconn'; | |
}else{ | |
console.log('Successfully connected to database.'); | |
} | |
}); | |
return db; | |
} | |
exports['@singleton'] = true; | |
exports['@require'] = [ 'config/settings' ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//// controllers/uploadHandler.js | |
// Look at this file first. | |
'use strict'; | |
var response, domain; | |
function uploadHandler(request, reply) { | |
domain = request.params.domain; | |
if(!response.res){ | |
Boom.badRequest(response); | |
}else{ | |
reply(response); | |
} | |
} | |
exports = module.exports = function(p) { // p is undefined althrough I @require'd 'models/Procedures' | |
p.checkExistComputer(domain, function (err, exists) { //error | |
//... | |
}); | |
return uploadHandler; | |
}; | |
exports['@singleton'] = true; | |
exports['@require'] = ['models/Procedures']; | |
/* | |
Error thrown: | |
c:\projects\nodejs\api\src\controllers\uploadHandler.js:16 | |
p.checkExistComputer(domain, function (err, exists) { | |
^ | |
TypeError: undefined is not a function | |
at module.exports (c:\projects\nodejs\api\src\controllers\uploadHandler.js:16:5) | |
at Singleton.instantiate (c:\projects\nodejs\api\node_modules\electrolyte\lib\patterns\singleton.js:18:23) | |
at Singleton.Component.create (c:\projects\nodejs\api\node_modules\electrolyte\lib\component.js:29:28) | |
at Container.create (c:\projects\nodejs\api\node_modules\electrolyte\lib\container.js:81:15) | |
at Object.<anonymous> (c:\projects\nodejs\api\src\config\routes.js:43:22) | |
at Module._compile (module.js:460:26) | |
at Object.Module._extensions..js (module.js:478:10) | |
at Module.load (module.js:355:32) | |
at Function.Module._load (module.js:310:12) | |
at Module.require (module.js:365:17) | |
at require (module.js:384:17) | |
at Object.fn (c:\projects\nodejs\api\node_modules\electrolyte\lib\loaders\node.js:24:12) | |
at Container._loadModule (c:\projects\nodejs\api\node_modules\electrolyte\lib\container.js:152:18) | |
at Container.create (c:\projects\nodejs\api\node_modules\electrolyte\lib\container.js:71:10) | |
at Object.<anonymous> (c:\projects\nodejs\api\server.js:27:18) | |
at Module._compile (module.js:460:26) | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
//// models/Procedures file | |
// | |
'use strict'; | |
//var ioc = require('electrolyte'); | |
var db; | |
//var database = ioc.create('config/database'); | |
var Procedures = function(database){ // Is 'config/database' even passed here? | |
db = database; | |
}; | |
Procedures.prototype.checkExistComputer = function(lcdomain, cb) { | |
/*SQL SCRIPT | |
SELECT domain FROM computers WHERE domain = 'test'; | |
*/ | |
if(db == 'notconn'){return cb(true, false, "No database connection.");} | |
var sql = "SELECT domain FROM computers WHERE domain = '" + lcdomain + "';"; | |
console.log(db); | |
var query = db.query(sql); | |
query.on("error", function (err) { | |
var comm = "<< [2] Database select " + err; | |
cb(true, false, comm); | |
}); | |
query.on("row", function (row, result) { | |
result.addRow(row); | |
}); | |
query.on('end', function (result) { | |
if (result.rows.length > 0) { | |
cb(false, true); | |
}else{ | |
cb(false, false); | |
} | |
}); | |
}; | |
exports = module.exports = Procedures; | |
exports['@literal'] = true; | |
exports['@require'] = ['config/database']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment