I'd like to create a model to handle everything related to users, starting with a findOne() function.
app.js:
var u = new User(client);
u.findOne(function(error, user) {
console.log(error, user);
});
models/User.js:
var User = function (client) {
this.client = client
};
User.prototype.findOne = function (id, callback) {
client.connect();
client.get('testkey', function(error, result) {
var test = "hello@world.com";
callback(null, test);
client.close();
});
};
module.exports = User;
node.js complains findOne() would be undefined.
What's the correct way of creating such models and providing them with objects, like database pools etc.?
Your code contains various errors:
new
when creating the instanceYou mixed a function with the object literal syntax:
var User = function (client) {
client: client
};
You want this.client = client;
instead. Right now the function body does nothing as it just defines a label called client
does nothing with the variable client
.
I would suggest you to search for an existing ORM for node.js instead of trying to write one on your own.