Nodejs and MongoDB Modules
Posted: Tue Apr 08, 2014 4:09 am
Okay so I have been racking my brain on this and I know I missing something simple. I am not using Mongoose or anything else other than mongojs.
All I want to do is hold my DB code outside of the app.js folder so i am using export
so in app.js
and in PlayerDB.js
Basically this will not work as returning a function gives the entire object. It is a function inside of a function so scope will not allow me to set a variable to pass back. What the heck am i doing wrong here? How is it done normally to use MongoDB inside of Nodejs and yet hold the code outside of the app.js?
All I want to do is hold my DB code outside of the app.js folder so i am using export
so in app.js
Code: Select all
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, PlayerDB = require('./functions/server/PlayerDB')
var databaseUrl = "chatarena";
var collections = ["Players"]
var db = require("mongojs").connect(databaseUrl, collections);
var Player = new PlayerDB();
console.log(Player.checkUser(db,"Halls"));
and in PlayerDB.js
Code: Select all
var PlayerDB = function (){
var self = this;
self.checkUser = function (db,Name)
{
db.Players.find({Name: Name}, function(err, Players)
{
if( err || !Players) return "No Players by that name";
else Players.forEach( function(Player) {
return Player.Strength;
} );
});
};
};
module.exports = PlayerDB;