[英]Javascript callback returns “undefined” using neo4j and meteor
for my Bachelor thesis I am required to build a Web frontend/ visualisation for a business process, using javascript. 对于我的学士论文,我需要使用javascript为业务流程构建Web前端/可视化。 This process is stored inside a neo4j database (version 2.1.7) and I am using meteor together with the driver, which is recommended on neo4j's website (version 2.10). 此过程存储在neo4j数据库(版本2.1.7)中,我与流星一起使用驱动程序,建议在neo4j的网站(版本2.10)上使用流星。
Since I am fairly new to Web development in general and especialy to javascript, I am not sure if my problem is of a technical or knowledge (aka me being a beginner) nature. 由于我通常对Web开发特别是对javascript尚不熟悉,因此我不确定我的问题是否属于技术或知识性质(又名我是初学者)。
At the moment I am working on a function, which should give me back a node from the database (currently "a node" equals a node's id, since the final representation of a node on the webpage is not yet decided). 目前,我正在开发一个函数,该函数应该从数据库中退回一个节点(当前“节点”等于节点的ID,因为尚未确定该节点在网页上的最终表示形式)。
getNodeById = function getNodeById(id){
console.log("The given id is " + id);
var result = undefined;
N4JDB.getNodeById(id, function getNode(node) {
console.log ("The node's id is " + node.id);
console.log("Result before " + result );
result = node.id;
console.log("Result after " +result);
});
return result;
}
Calling getNodeById(4) gives me the following output on meteor's console: 调用getNodeById(4)在流星的控制台上提供以下输出:
I20150309-16:03:32.200(1)? I20150309-16:03:32.200(1)? The given id is 4 I20150309-16:03:32.272(1)? 给定的ID是4 I20150309-16:03:32.272(1)? The node's id is undefined I20150309-16:03:32.272(1)? 节点的ID未定义I20150309-16:03:32.272(1)? Result before undefined I20150309-16:03:32.272(1)? 未定义I20150309-16:03:32.272(1)之前的结果? Result after undefined 未定义后的结果
The problem right now is, that I am not sure how to write a callback function, which fits for the drivers getNodeById function. 现在的问题是,我不确定如何编写适合驱动程序getNodeById函数的回调函数。
Link to the driver's documentation: https://atmospherejs.com/ostrio/neo4jdriver 链接到驱动程序的文档: https : //atmospherejs.com/ostrio/neo4jdriver
Edit:Further debugging has lead me to believe, that I make a mistake somewhere/how when invoking N4JDB.getNodeById since it always returns null 编辑:进一步的调试使我相信,调用N4JDB.getNodeById时,我总是在某处/如何出错,因为它总是返回null
New function (require statement on top of the file, since there will be many functions similar to this one)looks like this 新功能(文件顶部的require语句,因为会有很多与此功能相似的功能)看起来像这样
getNodeById = function getNodeById(id){
var myFuture = new Future ();
var result = undefined;
N4JDB.getNodeById(id, function getNode(node){
console.log(node);
result = node.id;
myFuture.return(result);
});
return myFuture.wait();
}
Final edit: It worked with 最终编辑:
getNodeById = function getNodeById(id){
var myFuture = new Future ();
var result = undefined;
N4JDB.getNodeById(id, function getNode(err,node){
//console.log(node);
result = node.id;
myFuture.return(result);
});
return myFuture.wait();
}
Meteor is based to node.js, so it works asynchronously. 流星基于node.js,因此它异步工作。 Your code starts to execute the function, but not waits for the result. 您的代码开始执行该功能,但不等待结果。
A good solution can be use the fibers/future npm package, as is explained at this link. 如此链接所述,可以使用fibre / future npm软件包来解决问题。 https://www.discovermeteor.com/patterns/5828399 https://www.discovermeteor.com/patterns/5828399
So: 所以:
getNodeById = function getNodeById(id){
Future = Npm.require('fibers/future');
var myFuture = new Future();
var result = undefined;
N4JDB.getNodeById(id, function getNode(node) {
result = node.id;
myFuture.return(result);
});
return myFuture.wait();
}
Hope it helps 希望能帮助到你
In case you're using Meteor's Neo4jDriver, you can simply run and execute Cypher query. 如果您使用的是Meteor的Neo4jDriver,则可以简单地运行和执行Cypher查询。
var foundByID = new ReactiveVar(null);
N4JDB.query('MATCH (s) WHERE id(s) = {myNodeID} RETURN s;', {myNodeID: 123}, function(error, result){
if(error){
//Throw error here
throw new Meteor.Error(500, error);
}
foundByID.set(result);
});
Or use Neo4jReactivity package 或使用Neo4jReactivity包
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.