在Neo4j中创建或更新节点的循环



我是Neo4j的新手,所以我非常坚持通过一些值循环。

我有一个技能到技能字符串的列表

let data = [
'big_data, business_intelligence',
'big_data, data_collection',
'big_data, economic_growth',
'big_data, economy'
]

我想创建或更新left side和right side

之间的关系
for (let item of data) {
CreateSkillToSkillRelation(item);
}
const CreateSkillToSkillRelation = async (relation) => {
let mainSkill = relation.split(",")[0];
let secundarySkill = relation.split(",")[1];
try {
// Check if the relationship exists
let { records } = await session.run(
"MATCH(main:SKILL {name:$mainSkill}) -[relation:SKILL_TO_SKILL]-> (secundary:SKILL {name:$secundarySkill}) RETURN relation",
{ mainSkill, secundarySkill }
);
let count =
records[0]?._fields[0].properties.count > 0
? records[0]._fields[0].properties.count + 1
: 1;
// If count is greater then 1 then lets update the counter
if (count > 1) {
await session.run(
"MATCH(main:SKILL {name:$mainSkill}) -[relation:SKILL_TO_SKILL]-> (secundary:SKILL {name:$secundarySkill}) SET relation.count = $count RETURN main, secundary",
{
mainSkill,
secundarySkill,
count,
}
);
}
// Otherwise the skill relation is not created so lets create one
else {
await session.run(
"CREATE(main:SKILL {name:$mainSkill}) -[:SKILL_TO_SKILL {count:$count}]-> (secundary:SKILL {name:$secundarySkill}) RETURN main, secundary",
{
mainSkill,
secundarySkill,
count,
}
);
}
await session.close();
} catch (error) {
console.log(error);
}
};

但是每次当我运行这个时,我都会得到以下错误Neo4jError: Queries cannot be run directly on a session with an open transaction; either run from within the transaction or use a different session.

你知道怎么解决这个问题吗?

for (let item of data) {
CreateSkillToSkillRelation(item);
}

没有等待你创建的承诺,所以你基本上是在一个只支持一个并发事务的会话上并发地运行所有这些承诺。

您应该在CreateSkillToSkillRelation的每个调用中创建一个会话,或者使用单个会话等待对它的每个调用。

虽然注意您在CreateSkillToSkillRelation结束时关闭会话,但仅在成功时关闭会话,我建议将await session.close();移动到finally块中。

  1. 同事@just_another_dotnet_dev的答案是绝对正确的:你在一个循环中运行异步函数,并在其中一个中关闭会话。

  2. Cipher语言非常丰富,你可以用它来做你在Javascript中尝试用循环做的所有事情。像这样,使用UNWINDMERGE:


const CreateSkillToSkillRelations = async (data) => {
const session = driver.session();
try {
let { records } = await session.run(
`WITH split(row, ',') as rels
WITH trim(rels[0]) as mainSkill, 
trim(rels[1]) as secundarySkill
MERGE (main:SKILL {name: mainSkill})
-[relation:SKILL_TO_SKILL]->
(secundary:SKILL {name: secundarySkill})
ON CREATE SET relation.count = 1
ON MATCH SET relation.count = relation.count + 1
RETURN main, relation, secundary`,
{ data }
);
} catch (error) {
console.log(error);
} finally {
await session.close()
}
};

相关内容

  • 没有找到相关文章

最新更新