thingdom / node-neo4j

[RETIRED] Neo4j graph database driver (REST API client) for Node.js
Apache License 2.0
926 stars 135 forks source link

Attributes in edges (relationships). #154

Closed brunocascio closed 9 years ago

brunocascio commented 9 years ago

i get this code:

   var query = [
        'MATCH (user:User)',
        'WHERE ID(user) = {uid}',
        'CREATE (recipe:Recipe {data})',
    ];

    // set ingredients
    var ingId;
    var unit;
    var cant;
    for (var i = data.ingredients.length - 1; i >= 0; i--) {
        ing   = data.ingredients[i];
        ingId = parseInt(ing.id);
        unit  = ing.unit;
        cant  = parseFloat(ing.cant);

        query.push(
            'WITH recipe',
            'MATCH (ingredient:Ingredient)',
            'WHERE ID(ingredient) = ' + ingId,
            'CREATE (recipe)-[ri:HAS {unit: "'+unit+'", cant: '+cant+'}]->(ingredient)'
        );    
    }

    query.push(
        'CREATE (user)-[:PREPARE]->(recipe)',
        'RETURN recipe'
    );

Produce:

MATCH (user:User)
WHERE ID(user) = {uid}
CREATE (recipe:Recipe {data})
WITH recipe
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = 7
CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]->(ingredient)
WITH recipe
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = 6
CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]->(ingredient)
CREATE (user)-[:PREPARE]->(recipe)
RETURN recipe

Basically create recipe with relations (author and ingredients)

If run in neo-client (rest) it work, but when run with this library i get this error:

{
"message": "[Statement.InvalidType] Collections containing mixed types can not be stored in   properties.",
"neo4j": {
"code": "Neo.ClientError.Statement.InvalidType",
"message": "Collections containing mixed types can not be stored in properties."
},
"name": "neo4j.ClientError"
}
aseemk commented 9 years ago

Sorry for the delay here! I somehow missed this. Did you manage to figure this out?

If not, the error message suggests that somehow one of your properties is an array, containing different types of elements. Not sure off the top of my head how that would happen given your code, but as a best practice anyway, it's really important to use proper query parameters, instead of injecting literal values into your raw query.

E.g.

CREATE (recipe)-[ri:HAS {riData}]->(ingredient)
{
    "riData": {"unit": "vasos", "cant": 1}
}

I see you're already using other query parameters, e.g. {uid} and the recipe node {data}, so hopefully it's easy for you to use it here too.

Either way, one debugging step then becomes that if you encounter this error, console.log the exact query and params that you sent. See if any have an array with mixed types.

aseemk commented 9 years ago

Feel free to re-open if this is still an issue.