尝试解释 Node-Neo4j API [英] Trying to interpret the Node-Neo4j API

查看:9
本文介绍了尝试解释 Node-Neo4j API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码很陌生,所以如果我的代码不可读或我的问题过于简单,请原谅我.

I'm pretty new to coding so forgive me if my code is unreadable or my question simplistic.

我正在尝试创建一个小的服务器应用程序(除其他外)显示 neo4j 节点的属性.我正在使用 node.js、Express 和 Aseem Kishore 的 Node-Neo4j REST API 客户端,其文档可以在 这里.

I am trying to create a little server application that (amongst other things) displays the properties of a neo4j node. I am using node.js, Express and Aseem Kishore's Node-Neo4j REST API client, the documentation for which can be found here.

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

My question stems from my inability to fetch the properties of nodes and paths. I can return a node or path, but they seem to be full of objects with which I cannot interact. I poured through the API documents looking for some examples of how particular methods are called but I found nothing.

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

Ive been trying to call the #toJSON method like, "db.toJSON(neoNode);" but it tells me that db does not contain that method. I've also tried, "var x = neoNode.data" but it returns undefined.

有人可以帮我解决这个问题吗?

Could someone please help me figure this out?

//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('
');
    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 属性?"的人回答我自己的问题.

Hey, I just wanted to answer my own question for anybody googling node, neo4j, data, or "How do I get neo4j properties?"

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

The gigantic object from neo4j, that when you stringified it you got all the "http://localhost:7474/db/data/node/7056/whatever" urls everywhere, that's JSON. You can query it with its own notation. You can set a variable to the value of a property like this:

var alpha = unfilteredResult[0]["nodes(p)"][i]._data.data;

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

Dealing with this JSON can be difficult. If you're anything like me, the object is way more complex than any internet example can prepare you for. You can see the structure by putting it through a JSON Viewer, but the important thing is that sometimes there's an extra, unnamed top layer to the object. That's why we call the zeroth layer with square bracket notation as such: unfilteredResult[0] The rest of the line mixes square and dot notation but it works. This is the final code for a function that calculates the shortest path between two nodes and loops through it. The final variables are passed into a Jade view.

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('
');
    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');

    });

};

推荐答案

我建议您查看我们为 Neo4j 2.0 更新的示例应用程序它使用 Cypher 加载数据和 Node-labels 来建模 Javascript 类型.

I would recommend that you look at the sample application, which we updated for Neo4j 2.0 Which uses Cypher to load the data and Node-labels to model the Javascript types.

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

请在查看此内容后提出更多问题.

Please ask more questions after looking at this.

这篇关于尝试解释 Node-Neo4j API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆