Hello,
Apologies in advance for any dumb questions, I am new to both javascript and to nodejs in general and am from a c# background.
I am considering using the singleton design pattern to handle the neo4j connection. However I am running into some trouble.
this is the code I am using for the singleton module, in a file called helper.js
var neo4j = require('neo4j');
var dbinstance;
function createDBInstance() {
var dbinstance = new neo4j.GraphDatabase('http://neo4j:welcome@localhost:7474');
}
module.exports.getDBInstance = function() {
if (!dbinstance) {
dbinstance = createDBInstance();
}
return dbinstance;
};I have another module for my controller in the file user.js
var db = require('.././Services/helper.js').getDBInstance();
db.cypher({
query: 'MATCH (user:User {email: {email}}) RETURN user',
params: {
email: "te...@test.com"
},
}, callback);
function callback(err, results) {
if (err) console.log(err);
var result = results[0];
if (!result) {
reply('No user found.');
} else {
var user = result['user'];
reply(user);
}
};
I get the following error.
[TypeError: Cannot read property 'cypher' of undefined]
Shouldn't the db object receive any methods including the cypher method from the singleton?
What am I missing?
Thank you and Regards,
Xavier