Reputation: 75
I am trying to add a node to a neo4j database in a node.js app and the code seems to execute fine, but no node is created.
Here is my code:
var neo4j = require('neo4j-driver');
// the rest of my program
const driver = new neo4j.driver(
"bolt://localhost:7687",
neo4j.auth.basic(neouser, neopass)
);
const session = driver.session();
var cypher =
"MERGE (b:TRIGGER {name:'" +
rname +
"'}) MERGE (c:FIELD{name:'" +
trigger +
"'}) MERGE (b)<-[:ACTIVATES]-(c)";
try {
const result = session.run(cypher);
console.log(result);
} finally {
console.log("finally");
session.close();
}
driver.close();
and the Error result:
Result {
_stack: '\n' +
' at captureStacktrace (C:\\NGToolbox\\NGToolBox\\node_modules\\neo4j-driver\\lib\\result.js:263:15)\n' +
' at new Result (C:\\NGToolbox\\NGToolBox\\node_modules\\neo4j-driver\\lib\\result.js:68:19)\n' +
' at Session._run (C:\\NGToolbox\\NGToolBox\\node_modules\\neo4j-driver\\lib\\session.js:174:14)\n' +
' at Session.run (C:\\NGToolbox\\NGToolBox\\node_modules\\neo4j-driver\\lib\\session.js:135:19)\n' +
' at C:\\NGToolbox\\NGToolBox\\routes\\bizrulevis.js:261:30\n' +
' at Layer.handle [as handle_request] (C:\\NGToolbox\\NGToolBox\\node_modules\\express\\lib\\router\\layer.js:95:5)\n' +
' at next (C:\\NGToolbox\\NGToolBox\\node_modules\\express\\lib\\router\\route.js:137:13)\n' +
' at Route.dispatch (C:\\NGToolbox\\NGToolBox\\node_modules\\express\\lib\\router\\route.js:112:3)\n' +
' at Layer.handle [as handle_request] (C:\\NGToolbox\\NGToolBox\\node_modules\\express\\lib\\router\\layer.js:95:5)\n' +
' at C:\\NGToolbox\\NGToolBox\\node_modules\\express\\lib\\router\\index.js:281:22',
_streamObserverPromise: Promise { <pending> },
_p: null,
_query: "MERGE (b:TRIGGER {name:'DELETEME.1'}) MERGE (c:FIELD{name:'4001'}) MERGE (b)<-[:ACTIVATES]-(c)",
_parameters: {},
_connectionHolder: ConnectionHolder {
_mode: 'WRITE',
_database: '',
_bookmark: Bookmark { _values: [] },
_connectionProvider: DirectConnectionProvider {
_id: 3,
_config: [Object],
_log: [NoOpLogger],
_userAgent: 'neo4j-javascript/0.0.0-dev',
_authToken: [Object],
_connectionPool: [Pool],
_openConnections: [Object],
_address: [ServerAddress]
},
_referenceCount: 1,
_connectionPromise: Promise { <pending> }
}
}
finally
When I go into Neo4j the db is still empty.
I have confirmed that it is active, that I can use that same cypher to create a node, that the connection is being established in the security log and it is not failing due to un/pw. Everything looks perfect, except for the whole nothing created issue.
Thank you for any advice.
edit: added the driver call and the driver close from outside the snippets scope.
Upvotes: 0
Views: 239
Reputation:
If you are writing some API, try to use an OGM https://www.npmjs.com/package/neo4j-node-ogm
class SomeModel extends from Model {} //see documentation
and to persist
const a = new SomeModel()
await a.save()
Tell me if that helps you.
Upvotes: 0
Reputation: 66927
This code should work for you (assuming neouser
, neopass
, rname
, and trigger
are defined):
const neo4j = require('neo4j-driver');
async function doIt() {
const driver = neo4j.driver(
"bolt://localhost",
neo4j.auth.basic(neouser, neopass)
);
const session = driver.session();
try {
await session.writeTransaction(async txc => {
var result = await txc.run(
`MERGE (b:TRIGGER {name: $rname})
MERGE (c:FIELD {name: $trigger})
MERGE (b)<-[:ACTIVATES]-(c)
RETURN b, c`,
{rname: rname, trigger: trigger}
)
result.records.map(record => {
console.log(`b: ${record.get('b')}, c: ${record.get('c')}`);
})
})
} finally {
console.log("finally");
await session.close();
await driver.close();
}
}
doIt();
This code is aware of the asynchronous nature of the processing, uses the result
after the transaction has completed, and only then closes the session and driver. It also uses a writeTransaction
to write to the DB.
You should study the driver's github documentation to get an idea of all the ways the driver can be used.
Upvotes: 1