aseemk / node-neo4j-template

A template app for using Neo4j from Node.js.
http://node-neo4j-template.herokuapp.com/
216 stars 92 forks source link

need a hint: how to query multiple nodes? #20

Closed graphadvantage closed 9 years ago

graphadvantage commented 9 years ago

Hi Aseem,

is there a simple way to return (and query the properties of) multiple nodes? Template works great for single nodes, looking for a method to handle multiple nodes from a cypher query. I can create callback variables for each node...seems like there should be a better way

User.get = function (username, callback) {
    var query = [
        'MATCH (user:User {username: {username}})-[:WATCHED]->(movie:MOVIE)',
        'RETURN user, movie',
    ].join('\n')
    var params = {
        username: username,
    };
    db.cypher({
        query: query,
        params: params,
    }, function (err, results) {
        if (err) return callback(err);
        if (!results.length) {
            err = new Error('No such user with username: ' + username);
            return callback(err);
        }
        var user = new User(results[0]['user']);
        var movie = new User(results[0]['movie']);
        callback(null, user, movie);
    });
};

Thanks! Michael

aseemk commented 9 years ago

Hey @qosmy-michael, sorry for the delay here!

results is an array of Cypher result rows, so if your Cypher query matches and returns multiple nodes, you can simply get all of them via that results array.

(So i.e. don't index [0]; instead, iterate over all results.)

You can find examples of this in User.getAll and User.prototype.getFollowingAndOthers.

Hope that helps! Feel free to re-open if something is still unclear.