我试图制作"配方","成分"和"用户"节点(作者)。
基本上,作者可以有很多食谱;和食谱与成分有多对多的关系。
我有这个问题:
MATCH (user:User)
WHERE ID(user) = {uid}
CREATE (recipe:Recipe {name:"receta 4"})
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(7)
CREATE (recipe)-[ri:HAS {unit: "vasos", cant: 1}]-(ingredient)
WITH ingredient
MATCH (ingredient:Ingredient)
WHERE ID(ingredient) = node(6)
CREATE (recipe)-[ri:HAS {unit: "cditas", cant: 2}]-(ingredient)
CREATE (user)-[:PREPARE]->(recipe)
RETURN recipe
但是,我得到错误:
ingredient not defined (line 4, column 7)
"WITH (ingredient:Ingredient)"
^
Neo.ClientError.Statement.InvalidSyntax
此查询的正确格式是什么?
我的js代码:
Recipe.create = function (req, callback) {
var data = req.body;
var uid = parseInt(req.params.id);
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 = ing.cant;
query.push(
'MATCH (ingredient:Ingredient)',
'WHERE ID(ingredient) = node(' + ingId + ')',
'CREATE (recipe)-[ri:HAS {unit: "'+unit+'", cant: '+cant+'}]-(ingredient)'
);
}
query.push(
'CREATE (user)-[:PREPARE]->(recipe)',
'RETURN recipe'
);
query.join("\n");
db.cypher({
query:query,
params:{
data: data,
uid: uid
}
}, function (err, results) {
if (err) return callback(err);
console.log(results)
callback(null, results);
});
};
答案 0 :(得分:2)
我认为问题在于WITH子句引用要创建的下一个节点,而不是引用要创建的上一个节点。此外,代码正在尝试创建无向关系。
试试这个:
MATCH (user:User) WHERE ID(user) = {uid} CREATE (recipe:Recipe {name:"receta 4"}) 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