Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-28-2019 03:12 PM
After running the Cypher query to create a new node if it doesn't exist using MERGE clause, I want to check if the write-transaction is successful.
How do I do that? Perhaps a status or code?
I'm getting following as result from the tx.run statement -
Preformatted textResult {
_stack:
'\n at captureStacktrace ( at new Promise (<anonymous>)',
_streamObserver:
_TransactionStreamObserver {
_fieldKeys: null,
_fieldLookup: null,
_queuedRecords: [],
_tail: null,
_error: null,
_hasFailed: false,
_observer: null,
_conn: null,
_meta: {},
_tx:
Transaction {
_connectionHolder: [ConnectionHolder],
_state: [Object],
_onClose: [Function: bound _transactionClosed],
_onBookmark: [Function: bound _updateBookmark] } },
_p: null,
_statement: 'match (u:user{firstName: \'abc\'}) return u',
_parameters: {},
_metaSupplier: [Function],
_connectionHolder:
EmptyConnectionHolder {
_mode: undefined,
_connectionProvider: undefined,
_referenceCount: 0,
_connectionPromise: Promise { null } } }
Thank you!
10-28-2019 06:10 PM
It looks like your statement there is 'match (u:user{firstName: \'abc\'}) return u'
-- so not a merge?
But to check the result (without returning anything), you'll know it is successful if it does not error.
var session = driver.session();
session.writeTransaction(function(tx) {
tx.run('MERGE (u:User {name: "Bob"})').then(function(result) {
console.log('No Error! It Worked!', result.summary.counters.nodesCreated());
}).catch(function(error) {
console.error('Query failed!', error);
}).finally(function() {
session.close();
})
});
I may have misunderstood your question? Feel free to clarify!
10-28-2019 11:50 PM
Thank you for your prompt response, Jacob.
You're right. Unfortunately, the dump was from a the MATCH clause instead of MERGE clause.
My code with MERGE clause is -
session.writeTransaction(function (transaction) {
transaction.run("merge (u:user{firstName:'" + firstName + "', lastName:'" + lastName + "', email:'" + email + "', githubUrl: '" + githubUrl + "', linkedinUrl:'" + linkedinUrl + "'}) return u");
// var result = transaction.run("match (u:user{firstName: '" + firstName + "'}) return u");
}).then(result => {
console.log("nodes created: ", result.summary.counters.nodesCreated());
}).catch(error => {
console.log('error: ', error)
}).finally(()=>{
session.close();
return res.json({ result });
});
However I'm getting error for summary being undefined. Stack-trace from the error is as given below.
Note that the required node is being added though.
result: Result {
_stack:
'\n at captureStacktrace ( at new Promise (<anonymous>)',
_streamObserver:
_TransactionStreamObserver {
_fieldKeys: null,
_fieldLookup: null,
_queuedRecords: [],
_tail: null,
_error: null,
_hasFailed: false,
_observer: null,
_conn: null,
_meta: {},
_tx:
Transaction {
_connectionHolder: [ConnectionHolder],
_state: [Object],
_onClose: [Function: bound _transactionClosed],
_onBookmark: [Function: bound _updateBookmark] } },
_p: null,
_statement:
'merge (u:user{firstName:\'abc\', lastName:\'dlisnc\', email:\'dvkdsnf\', githubUrl: \'fhsdiukn\', linkedinUrl:\'fsidnfk\'}) return u',
_parameters: {},
_metaSupplier: [Function],
_connectionHolder:
EmptyConnectionHolder {
_mode: undefined,
_connectionProvider: undefined,
_referenceCount: 0,
_connectionPromise: Promise { null } } }
error: TypeError: Cannot read property 'summary' of undefined
at session.writeTransaction.then.result (web-app\server\server.js:69:47)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:20316) UnhandledPromiseRejectionWarning: ReferenceError: result is not defined
at session.writeTransaction.then.catch.finally (web-app\server\server.js:74:27)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:20316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 1)
(node:20316) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
result: Result {
_stack:
'\n at captureStacktrace at new Promise (<anonymous>)',
_streamObserver:
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:19988) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 1)
(node:19988) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
------------------------------
error: TypeError: Cannot read property 'summary' of undefined
at session.writeTransaction.then.result (web-app\server\server.js:69:47)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:19988) UnhandledPromiseRejectionWarning: ReferenceError: result is not defined
at session.writeTransaction.then.catch.finally (web-app\server\server.js:74:27)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:19988) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 2)
Am I missing something here?
10-29-2019 04:50 AM
Ah I see, looks like you just need to add
return
To line with tx . Run. That should do it, as it stands it is not returning anything so promise resolved to undefined or null.
10-29-2019 12:25 PM
That worked!
Thanks a lot, Jacob!
All the sessions of the conference are now available online