【问题标题】:Trying to interpret the Node-Neo4j API试图解释 Node-Neo4j API
【发布时间】:2014-08-03 10:40:01
【问题描述】:

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

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

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

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

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

//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" url,这就是 JSON。你可以用它自己的符号来查询它。您可以将变量设置为属性的值,如下所示:

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

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

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

    });

};

【问题讨论】:

    标签: json node.js object neo4j


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2018-06-14
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多