Neo4j + nodejs:使用 javascript 对象文字创建节点
Neo4j + nodejs: create node using javascript object literal
找不到以前是否有人问过这个问题,请耐心等待。
我刚刚开始将 Neo4j 与 nodejs 后端和 neo4j-driver
驱动程序一起使用。我想知道是否可以创建一个具有多个属性的节点,而无需在 session.run
方法的第二个参数中枚举每个属性。
app.post("/signup", function(req, res) {
var user = req.body; //{userId: "johnsmith", email: "john@smith.com", ...}
session.run("CREATE (u:User {u}) RETURN u", user).then(function(response) {
/*do stuff with newly created user*/
}, function() {
//handle error
});
});
目前,这会产生以下错误:{code: 'Neo.ClientError.Statement.ParameterMissing', message: 'Expected a parameter named u' }
,如果我将以上内容更改为:
app.post("/signup", function(req, res) {
var user = req.body; //{userId: "johnsmith", email: "john@smith.com", ...}
session.run("CREATE (u:User {u}) RETURN u", {u: user}).then(function(response) {
/*do stuff with newly created user*/
}, function() {
//handle error
});
});
然后错误显示为:{ code: 'Neo.ClientError.Statement.TypeError', message: 'Property values can only be of primitive types or arrays thereof' }
。
这对我来说意义不大,因为 the refcard 明确指出您可以使用地图创建节点,如下所示:CREATE (n {map})
;所以我显然一定是弄错了。我希望我不必像这样枚举所有用户的属性:
session.run("CREATE (u:User {userId: {u.userId}, email: {u.email}, ...}) RETURN u", {u: user}).then(/*...*/)
提前致谢
- Map 不能是属性的值
- 您可以使用参数设置属性 - http://neo4j.com/docs/developer-manual/current/cypher/clauses/set/#set-set-all-properties-using-a-parameter
- 因此您需要检查输入参数并在必要时转换其属性。
例如:
app.post("/signup", function(req, res) {
var params = {};
//{userId: "johnsmith", email: "john@smith.com", ...}
Object.keys(req.body).forEach( function(k) {
var value = req.body[k];
if (!isPrimitive(val)) value = JSON.stringify(value);
params[k] = value;
});
session.run("CREATE (u:User) SET u = {user} RETURN u", {user: params})
.then(function(response) {
// do stuff with newly created user
}, function() {
// handle error
});
});
其中 isPrimitive
一个检查变量是否为原语的抽象函数。
Neo4j 仅支持将特定种类 的数据结构存储到属性。引用自Cypher Refcard:
Neo4j properties can be strings, numbers, booleans or arrays thereof.
并且,更准确地说,为了将数组(或 "collection")存储为 属性 值,其所有元素必须 相同原始类型。
来自@stdob 的答案-- 提供了一种可能的简单解决方法(但它会字符串化所有数组,甚至是无需转换即可存储的数组)。
注意:refacrd 需要更清楚一点。一般来说,嵌套 maps 受支持。例如,您可以自由地传入 JSON 数据作为 Cypher 查询参数。但是,不支持将包含嵌套地图的地图存储为 属性 值.
找不到以前是否有人问过这个问题,请耐心等待。
我刚刚开始将 Neo4j 与 nodejs 后端和 neo4j-driver
驱动程序一起使用。我想知道是否可以创建一个具有多个属性的节点,而无需在 session.run
方法的第二个参数中枚举每个属性。
app.post("/signup", function(req, res) {
var user = req.body; //{userId: "johnsmith", email: "john@smith.com", ...}
session.run("CREATE (u:User {u}) RETURN u", user).then(function(response) {
/*do stuff with newly created user*/
}, function() {
//handle error
});
});
目前,这会产生以下错误:{code: 'Neo.ClientError.Statement.ParameterMissing', message: 'Expected a parameter named u' }
,如果我将以上内容更改为:
app.post("/signup", function(req, res) {
var user = req.body; //{userId: "johnsmith", email: "john@smith.com", ...}
session.run("CREATE (u:User {u}) RETURN u", {u: user}).then(function(response) {
/*do stuff with newly created user*/
}, function() {
//handle error
});
});
然后错误显示为:{ code: 'Neo.ClientError.Statement.TypeError', message: 'Property values can only be of primitive types or arrays thereof' }
。
这对我来说意义不大,因为 the refcard 明确指出您可以使用地图创建节点,如下所示:CREATE (n {map})
;所以我显然一定是弄错了。我希望我不必像这样枚举所有用户的属性:
session.run("CREATE (u:User {userId: {u.userId}, email: {u.email}, ...}) RETURN u", {u: user}).then(/*...*/)
提前致谢
- Map 不能是属性的值
- 您可以使用参数设置属性 - http://neo4j.com/docs/developer-manual/current/cypher/clauses/set/#set-set-all-properties-using-a-parameter
- 因此您需要检查输入参数并在必要时转换其属性。
例如:
app.post("/signup", function(req, res) {
var params = {};
//{userId: "johnsmith", email: "john@smith.com", ...}
Object.keys(req.body).forEach( function(k) {
var value = req.body[k];
if (!isPrimitive(val)) value = JSON.stringify(value);
params[k] = value;
});
session.run("CREATE (u:User) SET u = {user} RETURN u", {user: params})
.then(function(response) {
// do stuff with newly created user
}, function() {
// handle error
});
});
其中 isPrimitive
一个检查变量是否为原语的抽象函数。
Neo4j 仅支持将特定种类 的数据结构存储到属性。引用自Cypher Refcard:
Neo4j properties can be strings, numbers, booleans or arrays thereof.
并且,更准确地说,为了将数组(或 "collection")存储为 属性 值,其所有元素必须 相同原始类型。
来自@stdob 的答案-- 提供了一种可能的简单解决方法(但它会字符串化所有数组,甚至是无需转换即可存储的数组)。
注意:refacrd 需要更清楚一点。一般来说,嵌套 maps 受支持。例如,您可以自由地传入 JSON 数据作为 Cypher 查询参数。但是,不支持将包含嵌套地图的地图存储为 属性 值.