I am using Neo4j which has a clause to allow setting multiple properties at once:
我正在使用Neo4j,它有一个允许同时设置多个属性的子句:
MATCH (p:Person)
WHERE p.email = '${userEmail}'
SET p += ${argsToUpdate}
RETURN p AS Person
However, I cannot get argsToUpdate
into the query because Neo4j is looking for a string exactly as it appears here:
但是,我无法将argsToUpdate输入到查询中,因为Neo4j正在寻找一个与这里显示的完全相同的字符串:
{ email: 'test@test.com', age: 1337, colour: 'blue' }
I could use JSON.stringify(argsToUpdate)
regex and strip \ and " but that is subpar because some properties could contain those characters. Maybe they could be encoded first.
我可以使用JSON.stringify(argsToUpdate) regex和条形\和“但是这不够,因为有些属性可能包含这些字符。”也许它们可以先被编码。
I also tried JSON.parse(JSON.stringify(argsToUpdate))
.
我也试过JSON.parse(JSON.stringify(argsToUpdate))。
I used util.inspect(argsToUpdate)
and it works perfectly, but my dev lead won't accept it because it's a node.js debugging method.
我使用了util.inspect(argsToUpdate),它工作得很好,但是我的dev lead不会接受它,因为它是一个节点。js调试方法。
Is there a simple way to achieve this another way?
有没有一种简单的方法来实现这一目标?
I don't want to do something like a for loop like this:
我不想像这样做一个for循环
let args = '{ '
for (let i = 0; i < Object.keys(argsToUpdate).length; i++) {
args += `${prop[i]}: ${key[i]}`
}
args += ' }'
Is there an efficient way to do this? The issue is that argsToUpdate
contains a variable number of properties. I want it to be dynamic so that the code isn't so brittle. Is there anything available in ES6+ including babel that can achieve the result of util.inspect(argsToUpdate)
?
有没有一种有效的方法来做到这一点?问题是argsToUpdate包含了数量可变的属性。我希望它是动态的,这样代码就不会那么脆弱。ES6+中是否有包括babel可以实现util.inspect(argsToUpdate)的结果?
1 个解决方案
#1
1
Assuming you're using the default neo4j driver, you should be able to pass all parameters to the query in standard javascript data structures and not worry about string escaping like this. I think the issue is that your parameters are named incorrectly.
假设您正在使用默认的neo4j驱动程序,那么您应该能够在标准的javascript数据结构中将所有参数传递给查询,而不必担心像这样的字符串转义。我认为问题是您的参数命名不正确。
Cypher parameters must be composed of unicode letters and cannot start with a currency symbol. Names enclosed in backticks can contain spaces. So, should take forms like the below.
Cypher参数必须由unicode字母组成,不能以货币符号开始。在回签中包含的名称可以包含空格。因此,应该采取如下形式。
{someParam}
- { someParam }
$someParam
- someParam美元
Or even something like the two below.
甚至是下面这两个。
{ `some param with spaces in the name` }
$`some param with spaces in the name`
With that in mind, the query below should work.
考虑到这一点,下面的查询应该可以工作。
session
.run('MATCH (p:Person) WHERE p.email = $userEmail SET p += $argsToUpdate RETURN p AS Person', {userEmail: 'test@test.com', argsToUpdate: { email: 'test@test.com', age: 1337, colour: 'blue' }})
#1
1
Assuming you're using the default neo4j driver, you should be able to pass all parameters to the query in standard javascript data structures and not worry about string escaping like this. I think the issue is that your parameters are named incorrectly.
假设您正在使用默认的neo4j驱动程序,那么您应该能够在标准的javascript数据结构中将所有参数传递给查询,而不必担心像这样的字符串转义。我认为问题是您的参数命名不正确。
Cypher parameters must be composed of unicode letters and cannot start with a currency symbol. Names enclosed in backticks can contain spaces. So, should take forms like the below.
Cypher参数必须由unicode字母组成,不能以货币符号开始。在回签中包含的名称可以包含空格。因此,应该采取如下形式。
{someParam}
- { someParam }
$someParam
- someParam美元
Or even something like the two below.
甚至是下面这两个。
{ `some param with spaces in the name` }
$`some param with spaces in the name`
With that in mind, the query below should work.
考虑到这一点,下面的查询应该可以工作。
session
.run('MATCH (p:Person) WHERE p.email = $userEmail SET p += $argsToUpdate RETURN p AS Person', {userEmail: 'test@test.com', argsToUpdate: { email: 'test@test.com', age: 1337, colour: 'blue' }})