[英]Connecting to neo4j in Nodejs
I tried to run a simple code connecting Neo4j with Java script, But I don't know why there isn't any change in database, I tried a verity type of connection.我试图运行一个简单的代码连接 Neo4j 和 Java 脚本,但我不知道为什么数据库没有任何变化,我尝试了一种真实类型的连接。 You can see the code in the following:
您可以看到以下代码:
const neo4j = require('neo4j-driver')
//const driver = neo4j.driver("neo4j://localhost:7687", neo4j.auth.basic("neo4j", "qaz"));
const driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "qaz"));
const session = driver.session()
const personName = 'Test'
try {
const result = session.run(
'CREATE (a:Person {name: $name}) RETURN a',
{ name: personName }
)
const singleRecord = result.records[0]
const node = singleRecord.get(0)
console.log(node.properties.name)
}
finally {
session.close()
}
// on application exit: driver.close() // 在应用程序退出时:driver.close()
Even I tried the online database, but I had the problem still.即使我尝试了在线数据库,但问题仍然存在。 Also I couldn't receive any error or warning.
我也没有收到任何错误或警告。 Any idea?
任何的想法?
I would suggest you try to catch errors and see what comes out.我建议您尝试捕获错误并查看结果。 The only problem I imagine seeing is that you use "neu4j" username instead of the "neo4j".
我想看到的唯一问题是您使用“neu4j”用户名而不是“neo4j”。
Anyway, the best practice is to catch errors to help you with the debugging.无论如何,最佳实践是捕获错误以帮助您进行调试。
// Edit: // 编辑:
It seems that you are not awaiting the results of the Neo4j session as JS is all about async :) Try to use the then
clause like in the documentation:似乎您没有等待 Neo4j 会话的结果,因为 JS 都是关于异步的 :) 尝试使用文档中的
then
子句:
session
.run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
nameParam: 'James'
})
.then(result => {
result.records.forEach(record => {
console.log(record.get('name'))
})
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
I finally found how to solve it我终于找到了解决方法
async function test(){
const neo4j = require('neo4j-driver')
const driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "qaz"))
const session = driver.session()
const personName = 'Test'
try {
const result = await session.run(
'CREATE (a:Person {name: $name}) RETURN a',
{ name: personName }
)
const singleRecord = result.records[0]
const node = singleRecord.get(0)
console.log(node.properties.name)
} finally {
await session.close()
}
// on application exit:
await driver.close()
}
test();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.