首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试解释Node-Neo4j API

尝试解释Node-Neo4j API
EN

Stack Overflow用户
提问于 2014-06-13 07:50:39
回答 1查看 734关注 0票数 0

我是一个编程新手,所以如果我的代码无法阅读或者我的问题过于简单,请原谅我。

我正在尝试创建一个小服务器应用程序,用于显示neo4j节点的属性(以及其他内容)。我使用的是node.js、Express和Aseem的Node-Neo4jrest API客户端,其文档可以在here.中找到

我的问题源于我无法获取节点和路径的属性。我可以返回一个节点或路径,但它们似乎充满了我无法交互的对象。我翻阅了API文档,寻找如何调用特定方法的示例,但一无所获。

我一直在尝试调用#toJSON方法,比如"db.toJSON(neoNode);“,但它告诉我db不包含该方法。我也尝试过,"var x= neoNode.data“,但返回未定义。

有没有人能帮我解决这个问题?

代码语言:javascript
复制
//This file accepts POST data to the "queryanode" module
//and sends it to "talkToNeo" which queries the neo4j database.
//The results are sent to "resultants" where they are posted to
//a Jade view. Unfortuantly, the data comes out looking like 
// [object Object] or a huge long string, or simply undefined.



var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://localhost:7474');



function resultants(neoNode, res){
    // if I console.log(neoNode) here, I now get the 4 digit integer
    // that Neo4j uses as handles for nodes. 
    console.log("second call of neoNode" + neoNode);
    var alpha = neoNode.data; //this just doesn't work
    console.log("alpha is: " +alpha); //returns undefined
    var beta = JSON.stringify(alpha);

    console.log("logging the node: ");
    console.log(beta);// still undefined
    res.render("results",{path: beta});
    res.end('end');

}


function talkToNeo (reqnode, res) {
    var params = {
    };
    var query = [
    'MATCH (a {xml_id:"'+ reqnode +'"})',
    'RETURN (a)' 
    ].join('\n');
    console.log(query);

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

        var neoNode = results.map(function (result){
            return result['a']; //this returns a long string, looks like an array,
                                //but the values cannot be fetched out
        });
        console.log("this is the value of neoNode");
        console.log(neoNode);
        resultants(neoNode, res);
    });

};


exports.queryanode = function (req, res) {
    console.log('queryanode called');
    if (req.method =='POST'){
        var reqnode = req.body.node;    //this works as it should, the neo4j query passes in
        talkToNeo(reqnode, res)         //the right value.
    }
}

编辑

嘿,我只是想为那些用谷歌搜索节点、neo4j、数据或“如何获取neo4j属性?”的人回答我自己的问题。

来自neo4j的巨大对象,当你对它进行字符串化时,到处都是"http://localhost:7474/db/data/node/7056/whatever" urls,这就是JSON。你可以用它自己的符号来查询它。可以将变量设置为属性的值,如下所示:

代码语言:javascript
复制
var alpha = unfilteredResult[0]["nodes(p)"][i]._data.data;

处理这个JSON可能很困难。如果你像我一样,这个对象比任何互联网例子都要复杂得多。您可以通过JSON Viewer,查看结构,但重要的是,有时对象有一个额外的、未命名的顶层。这就是为什么我们称第0层为使用方括号表示法:unfilteredResult[0],该行的其余部分混合了正方形和点表示法,但它是有效的。这是一个函数的最终代码,该函数计算两个节点之间的最短路径并循环通过它。最后的变量被传递到一个Jade视图中。

代码语言:javascript
复制
function talkToNeo (nodeone, nodetwo, res) {
    var params = {
    };
    var query = [
    'MATCH (a {xml_id:"'+ nodeone +'"}),(b {xml_id:"' + nodetwo + '"}),',
    'p = shortestPath((a)-[*..15]-(b))',
    'RETURN nodes(p), p' 
    ].join('\n');
    console.log("logging the query" +query);


    db.query(query, params, function (err, results) {
        if (err) throw err;
        var unfilteredResult = results;


        var neoPath = "Here are all the nodes that make up this path: ";
        for( i=0; i<unfilteredResult[0]["nodes(p)"].length; i++) {
            neoPath += JSON.stringify(unfilteredResult[0]['nodes(p)'][i]._data.data);
        }  


        var pathLength = unfilteredResult[0].p._length;
        console.log("final result" + (neoPath));
        res.render("results",{path: neoPath, pathLength: pathLength});
        res.end('end');

    });

};
EN

回答 1

Stack Overflow用户

发布于 2014-06-15 03:09:25

我建议您查看示例应用程序,它是我们为Neo4j 2.0更新的,它使用Cypher2.0加载数据和节点标签来模拟Javascript型。

你可以在这里找到它:https://github.com/neo4j-contrib/node-neo4j-template

请在看完这篇文章后提出更多问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24195685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档