Given a node, I want to query for any incoming relationships for that node and return nodes that have that relationship. I am modifying Node Neoj4 Template
I am expecting to get back an array of nodes, create objects for each element and then reference the nodes properties. Instead I get back empty objects. I do, however get the correct number of nodes for the query (i.e. 2 inbound relationships, returns 2 elements).
I have run the query in the cypher shell and I get the results that I expect. I am using Neo4j 1.9.2
About the Graph: I have a genealogy graph consisting of 8 People nodes and 2 relationship types: INHERITS_X, INHERITS_Y. This refers to the the chromosomes that determine sex. For example, if I have two children then I have 2 incoming relationships; my son INHERITS_Y and my daughter INHERITS_X.
Any help appreciated.
Thanks,
Here is where the query function (getInbound) is called:
exports.show = function (req, res, next) {
Person.get(req.params.id, function (err, person) {
if (err) return next(err);
person.getInbound(function (err, inbound) {
res.render('person', {
person: person
})
})
})
};
And the getInbound function:
Person.prototype.getInbound = function(callback) {
var query = ['START p=node({ID})',
'MATCH p <-[:INHERITS_Y|INHERITS_X]- m',
'RETURN m'
].join('\n');
var params = {
ID: this.id,
NAME: this.name
};
console.log("In getInbound function");
console.log("this.name is ",this.name);
var in_nodes = [];
db.query(query, params, function (err, results) {
console.log("Error from the query ",err);
if (err) return callback(err);
console.log("Number of elements in array returned from query: ", results.length);
for (var i=0; i< results.length; i++) {
console.log(" In the for loop, var i = ",i);
var in_node = new Person(results[i]);
console.log("New Person object created from array element ", in_node);
in_nodes.push(in_node);
}
callback(null, in_nodes);
});
};
Output from running the code:
Express server listening on port 5000
GET /persons 304 77ms
IN getInbound - this.name is Eddie Dickey
Error from the query null
results length returned from query: 2
In the for loop, var i = 0
Creating a new Person object form each element of array returned from query { _node: { m: { db: [Object], _request: [Object], _data: [Object] } } }
In the for loop, var i = 1
Creating a new Person object form each element of array returned from query { _node: { m: { db: [Object], _request: [Object], _data: [Object] } } } GET /persons/0 200 21ms - 602b
And the Person constructor:
var Person = module.exports = function Person(_node) {
// all we'll really store is the node; the rest of our properties will be
// derivable or just pass-through properties (see below).
this._node = _node;
}
Cypher query results are never just an array of nodes -- they're an array of results, where a "result" is a dictionary of variable name to the corresponding (node/rel/path/etc.) object.
So in your example, your Cypher query says RETURN m
, so in your for
loop, you need to access results[i]['m']
, not just results[i]
.
Hope this helps.