给定一个节点,我想查询该节点的任何传入关系并返回具有该关系的节点。我正在修改节点 Neoj4 模板
我期望取回一个节点数组,为每个元素创建对象,然后引用节点属性。相反,我得到了空对象。但是,我确实获得了查询的正确节点数(即 2 个入站关系,返回 2 个元素)。
我已经在 cypher shell 中运行了查询,得到了我期望的结果。我正在使用 Neo4j 1.9.2
关于图:我有一个由 8 个人员节点和 2 种关系类型组成的家谱图:INHERITS_X、INHERITS_Y。这是指决定性别的染色体。例如,如果我有两个孩子,那么我有 2 个传入关系;我的儿子 INHERITS_Y 和我的女儿 INHERITS_X。
任何帮助表示赞赏。
谢谢,
这里是调用查询函数 (getInbound) 的地方:
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
})
})
})
};
和 getInbound 函数:
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);
});
};
运行代码的输出:
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
和 Person 构造函数:
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;
}