Singleton with Node-Neo4J

107 views
Skip to first unread message

Xavier Thomas

unread,
Oct 27, 2015, 9:32:41 AM10/27/15
to Node-Neo4j
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

Aseem Kishore

unread,
Oct 27, 2015, 9:35:34 AM10/27/15
to Xavier Thomas, Node-Neo4j
You're simply forgetting the `return` in `getDBInstance`.

You can debug that by the error message telling you that `db.cypher` is trying to read `cypher` from `undefined`, which means `db` is undefined. And `db` came from that method.

Cheers,
Aseem

Xavier Thomas

unread,
Oct 28, 2015, 1:10:36 AM10/28/15
to Node-Neo4j
yep, that fixed it.
feel pretty stupid for missing something that trivial.

Thanks again for you help, Aseem.
Reply all
Reply to author
Forward
0 new messages