Search code examples
javascriptdatabaseneo4junderscore.jsnode-neo4j

JS functional object constructor - Neo4j Database has no method 'query'


I'm using the node-neo4j library along with node.js, underscore and prototype.

I'm trying to have a model which extends a database adapter. Code first.

BaseModel.js:

var _ = require("underscore"),
    Neo4jAdapter = require('../adapters/neo4j/Adapter');

function BaseModel() {

    _.extend(this, new Neo4jAdapter());
};

module.exports = BaseModel;

Neo4jAdapter.js:

var _ = require("underscore"),
    Neo4j = require('neo4j');

function Neo4jAdapter() {

    this.db = new Neo4j.GraphDatabase('http://localhost:3000');
};

Neo4jAdapter.prototype.insert = function(label, data, callback) {

    console.log('Neo4jAdapter', 'Attempt to insert node');

    if (label == '') throw 'Label is not defined';

    var query = [
        'CREATE (n:LABEL {mdata})',
        'RETURN n'
    ].join('\n').replace('LABEL', label);

    this.db.query(query, data, function (err, results) {
        if (err) throw err;

        console.log(results);

        var result = results[0].n._data.data;
        result.id = results[0].n._data.metadata.id;

        callback(result);
    });
};

module.exports = Neo4jAdapter;

The weird thing is the error I'm getting. I won't post all the node.js / express code here, but I'm hitting the insert function with an url. The response I get is the following:

Message: Object #

GraphDatabase has no method 'query'

Error: TypeError: Object #

GraphDatabase has no method 'query'

My question is: Why does the database object does not have the function query(), even tho the docs are saying it should have one?

Possible cause: I bet the adapter's db object is not populated yet when calling the insert method, but how do I do that?

Thanks


Solution

  • You have probably unintentionally installed the newer, alpha version.

    See this issue for a similar problem: https://github.com/thingdom/node-neo4j/issues/150

    Hi @Christopheraburns! Run this for me:

    npm ls neo4j

    I'm guessing you've installed node-neo4j v2 (we have alpha versions in npm currently), which has breaking changes to the API. The output of that npm ls will tell you if you have node-neo4j v1 or v2 installed (e.g. 1.1.1 vs. 2.0.0-alpha3).

    You can downgrade to 1.1.1 by doing:

    npm uninstall neo4j npm install neo4j@1.1.1 --save

    Alternately, if you're interested in v2 (still a WIP! but almost finished, and pretty stable), here are some temporary links until this gets merged to master:

    API docs (WIP): https://github.com/thingdom/node-neo4j/blob/v2/API_v2.md

    Hope this helps! Feel free to re-open if this doesn't resolve things for you.

    So, you probably need to uninstall neo4j and reinstall the older version, or take a look at the cypher method in v2.

    https://github.com/thingdom/node-neo4j/blob/v2/API_v2.md#cypher