Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

小白请教一个函数调用问题,异步不太懂

$
0
0
var getChild = function(rootName) {

    var cypher = "Match (n:Country {name:'" + rootName + "'})-[:children]->(m) return m";

    db.cypherQuery(cypher, function(err, result) {
        if (err) throw err;
        return result;
    });
}

var initJson = function(rootName, parent) {
    var result = getChild(rootName);

    if (result.data.length != 0) {
        //TODO
        var children = [];
        for (var i = 0; i < result.data.length; i++) {
            children[i] = { name: result.data[i].name };
            initJson(result.data[i].name, children[i]);
        }
        parent.children = children;
        return;
    } else {
        //TODO
        return;
    }
}
var createJson = function(rootName) {
    json.name = rootName;
    var parent = json;
    initJson(rootName, parent);
    console.log(JSON.stringify(json));
}
router.get('/', function(req, res) {
    function_one();
    res.render('index', { title: "Express" });
});

在initJson调用getChild的时候得到的返回都是undefined,我知道在getchild里面没返回出来,但是现在也没找到合适的办法,希望能有人能帮我解答一下,谢谢。


Viewing all articles
Browse latest Browse all 14821

Trending Articles