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

_node issue - Error creating relationships #4

Closed rorymadden closed 11 years ago

rorymadden commented 11 years ago

Hi, I have set up a simple app to test out creating nodes and relationships in Neo4j. https://github.com/rorymadden/neo4j-test I am able to create nodes ok but I am having difficulties creating a relationship.

The relationship is failing with an error message of Error: Failed to create relationship I have based my code on the node-neo4j-template and tried to see what difference there is with my code. This is where I am getting very confused. The relevant code is below:

User.prototype.follow = function (other, callback) {
    console.log('other node: '+other._node);
    this._node.createRelationshipTo(other._node, 'follows', {}, function (err, rel) {
        callback(err);
    });
};

I have added in a console.log to inspect the structure of the other node. If I JSON.stringify and print out other._node I get identical syntax to my code. However when I print other._node on its own I get a difference. In my code I get [object Object] but with the node-neo4j-template code I get 'node @15'. I believe that this is what is causing the relationship to fail.

The main difference between the two apps is that I have more than one node type (server model). I have users and projects. The only thing I can think of is that I am creating a new DB instance in each model which may cause the difference. However can you please elaborate on how _other.node prints out as node @15? THis may help me to resolve the problem.

Thanks for your help

aseemk commented 11 years ago

Hey @rorymadden, sorry to hear you're having this issue! Let's see if we can figure it out.

To start, the node @15 is simply coming from the Node::toString() method:

https://github.com/thingdom/node-neo4j/blob/0.2.19/lib/Node._coffee#L27-L34

One thing to be aware of that is when you run console.log(obj) in Node, the object's toString() doesn't get called -- you have to explicitly concatenate the object with a string to get that, e.g. console.log('obj: ' + obj). It seems you're already doing that in your code sample above -- so are you saying you're seeing other node: [object Object]?

As for the error you're seeing, I took a look at the code to see when we throw that error:

https://github.com/thingdom/node-neo4j/blob/0.2.19/lib/Node._coffee#L186-L235

And it seems to happen if either node doesn't have a Node property we expect on it. (The error message could/should be improved to state this.) So could you share how you're getting both nodes? (E.g. if you're creating either via db.createNode(), has it been saved yet? Or are they coming from a Cypher query? etc.)

Hope this helps!

rorymadden commented 11 years ago

Thanks for the quick reply. It was a simple mistake on my part. I am trying to use Backbone so the data that is being presented to the front end is pure JSON. There are no methods available, like the toString method. To resolve the problem I have created a toJSON method on my model which returns just the id and the data. Whenever I need to update a model I pass in the ID and have to look up the node in Neo4j before performing any actions on it. It adds a bit of overhead but at least its working.

Thanks for your help.

aseemk commented 11 years ago

Glad to hear it. I don't know of any other way you would do it (after all, you don't want the browser client to have direct access to the database), so fear not — you're doing it correctly now. =)