我正在
"预期名为paramEmail"
的参数
当我在Neo4j浏览器中执行以下cypher查询时。
CREATE (user:Owner {
email:{paramEmail},
password:{paramPassword},
name:{paramName},
category:{paramCategory}})
RETURN properties(user),
{
paramEmail: "555@ali.com",
paramPassword: "5555555",
paramName: "Tony",
paramCategory: "Category"
}
根据我的所有检查,这是正确的语法....我在这里缺少什么......另外,有没有更好的方法来呈现参数?
params: {parama:a,paramb:b,paramc:c}
没有工作。 好的...所以我做了一些挖掘,发现javascript中的语法实际上来自我修复的.run,但现在我从第二个.then或.catch(无效令牌)得到了同样的错误。很抱歉,如果我看起来很基本,但我是Neo4j的新手,特别是v3.0,会话块被证明有点麻烦。有人可以告诉我块的错误:
neo4jSession
.run ("MATCH (user {email : {paramEmail}}) RETURN count(user)=1 as user_exists",{ paramEmail: newUser.email})
.then (function(result) {
console.log("Neo4j Session started");
if ( result.records[0].get('user_exists') !== true )
{
neo4jSession
.run("CREATE (user:Owner {email:{paramEmail}, password:{paramPassword}, name:{paramName}, category:{paramCategory}}) RETURN properties (user)",
{params: {
paramEmail: newUser.email,
paramPassword: newUser.password,
paramName: newUser.name,
paramCategory: newUser.category
}})
// .run (CREATE (user:newUser.category {email: {newUser.email}, password: {newUser.password},
// name: {newUser.name}, mobilenumber: {newUser.mobilenumber}) ASSERT newUser.email is UNIQUE RETURN properties (user));
console.log( result.records[0].get("email") + " " + result.records[0].get("name") );
neo4jSession.close();
}
else // (found)
{
// email address already exist
console.log("email address already exist");
neo4jSession.close();
} //end o f else
//catch any system errors
.then (function(error)
{
neo4jSession.close();
console.log(error);
});
}) // end of .then
抱歉......我想我也应该向你们展示数据库设置......见下文:
var neo4j = require('neo4j-driver').v1;
var neo4jdb = neo4j.driver("bolt://localhost:7474", neo4j.auth.basic("neo4j", "neo4j"),
{
trust: "TRUST_ON_FIRST_USE",
encrypted:true
});
var neo4jSession =neo4jdb.session();
最后通过移动一些大括号来消除会话块中的语法错误"}"和parens")" ....仍然不太了解它但我理解逻辑....所以代码编译但代码的那部分显然没有执行!! ..代码显然停在" neo4jSession"之前的那一行。紧跟在" console.log之后(" Neo4j会议即将开始")...我已经在下面包含了修改后的代码:
console.log("Neo4j Session about to start")
//start neo4j session
neo4jSession
.run ("MATCH (user {email : {paramEmail}}) RETURN count(user)=1 as user_exists",{ paramEmail: newUser.email})
.then (function(result) {
console.log("Neo4j Session started")
if ( result.records[0].get('user_exists') !== true )
{
neo4jSession
.run("CREATE (user:Owner {email:{paramEmail}, password:{paramPassword}, name:{paramName},mobilenumber:{parammobileNumber}, category:{paramCategory}}) RETURN user",
{params: {
paramEmail: newUser.email,
paramPassword: newUser.password,
paramName: newUser.name,
parammobileNumber: newUser.mobilenumber,
paramCategory: newUser.category
}})
.then (function(result)
{
console.log(user.name);
neo4jSession.close();
})
.then (function(error)
{
neo4jSession.close();
console.log(error);
})
// .run (CREATE (user:newUser.category {email: {newUser.email}, password: {newUser.password},
// name: {newUser.name}, mobilenumber: {newUser.mobilenumber}) ASSERT newUser.email is UNIQUE RETURN properties (user));
console.log( result.records[0].get("email") + " " + result.records[0].get("name") );
neo4jSession.close();
}
else // (found)
{
// email address already exist
console.log("email address already exist");
neo4jSession.close();
} //end o f else
}) // end of .then
//catch any system errors
.then (function(error)
{
neo4jSession.close();
console.log(error);
});
答案 0 :(得分:2)
除非我最近发现一些我不知道的事情,否则Web界面不支持参数(尽管neo4j shell支持这些参数,并且以编程方式支持)。
答案 1 :(得分:0)
但是cycli客户端中的neo4j-shell支持参数(您必须安装zip分发版)。
只是做
export paramEmail=my@email.com
然后运行您的查询所有shell变量将作为参数传入。
您可以使用env
列出它们。
答案 2 :(得分:0)
首先澄清一下,通过这里使用的javascript neo4j-driver来初始化与neo4j数据库的连接的代码。
var neo4j = require('neo4j-driver').v1
var neo4j_driver = neo4j.driver("bolt://localhost",
neo4j.auth.basic("neo4j", "neo4j"),
{
trust: "TRUST_ON_FIRST_USE",
encrypted: true
});
var neo4jSession = neo4j_driver.session();
应保存在数据库中的新用户对象:
var newUser = {
email: "max@somewhere.org",
password: "s3cr3t",
name: "Max",
mobilenumber: "555-12345",
category: "someCategory"
}
查询如下所示
session.run(query, [pObject])
.then(resultCallback)
.catch(errorCallback);
其中query
是一个字符串(密码查询),pObject
是一个可选对象,其键与查询字符串中的名称相匹配,例如'MATCH (u {name: "Max") RETURN u'
将使用参数query = 'MATCH (u {name: { pName }}) RETURN u'
和pObject = { pName: "Max"}
。
如果返回结果,则会调用resultCallback
,错误为errorCallback
。
函数resultCallback
接受一个参数,进一步称为result
。然后从第一条记录中获取字段为result.record[0].get('sthing')
,其中sthing
与查询RETURN sthing
中的变量匹配。 e.g。
query = '... RETURN user, object.name, count(sth) as sth_count'
...
// in resultCallback
r = result.record[0]
// right
r.get('user');
r.get('user').name;
r.get('object.name');
r.get('sth_count')
// wrong
r.get('user.name');
r.get('count(sth)');
r.get('sth');
函数errorCallback
接受一个参数,进一步称为error
,其中包含已发生错误的描述。
session.close()
。
调整后的示例如下所示:
var neo4j = require('neo4j-driver').v1
var neo4j_driver = neo4j.driver("bolt://localhost",
neo4j.auth.basic("neo4j", "neo4j"),
{
trust: "TRUST_ON_FIRST_USE",
encrypted: true
});
var neo4jSession = neo4j_driver.session();
var newUser = {
email: "max@somewheref.org",
password: "s3cr3t",
name: "Max",
mobilenumber: "555-12345",
category: "someCategory"
}
//start neo4j session
console.log("Neo4j Session about to start");
neo4jSession.run(
// query has to be a string
"MATCH (user {email : {paramEmail}}) RETURN count(user)=1 as user_exists",
// parameter Object with keys match those in the query string
{ paramEmail: newUser.email }
)
// result callback for outer query
.then(function(result) {
console.log("Neo4j Session started");
console.log(result);
// parameter to get is the same string as one of the RETURNed variables.
console.log(result.records[0].get('user_exists'));
if (result.records[0].get('user_exists') !== true) {
neo4jSession.run(
"CREATE (user:Owner {email:{paramEmail}, password:{paramPassword}, name:{paramName}, mobilenumber:{parammobileNumber}, category:{paramCategory}}) RETURN user", // query has to be a string
{ // parameter Object with keys match those in the query string
paramEmail: newUser.email,
paramPassword: newUser.password,
paramName: newUser.name,
parammobileNumber: newUser.mobilenumber,
paramCategory: newUser.category
}
)
// result callback for inner query
.then(function(result) { // for results use .then(...)
// parameter to get is the same string as one of the RETURNed variables.
console.log("New user" + result.records[0].get("user"));
neo4jSession.close();
})
.catch(function(error) { //for errors use .catch(...)
console.log("Inner query error");
console.log(error);
neo4jSession.close();
})
} else // (found)
{
// email address already exist
console.log("email address already exist");
} // end of else
neo4jSession.close();
}) // end of .then
//catch any system errors of outer query
.catch(function(error) {
console.log("Outer query error");
console.log(error);
neo4jSession.close();
});
P.S。:newUser
对象可以直接用作查询参数对象,只需调整查询字符串中的变量名称以匹配newUser
的键。您甚至可以传递newUser
对象,并将键作为属性名称和值使用,如下所示:
query = "CREATE (user:Owner {params}) RETURN user";
pObject = {params: newUser};
session.run(query, pObject).then(...).catch(...);