Your challenge is to modify another pre-written file to add yourself as an actor in The Matrix.
Steps
-
Update the
params
object to use your name. This step isn’t strictly required, just a bit of fun. -
The Cypher statement is already written for you. Call the
session.executeWrite()
method, passing a callback function to represent the unit of work. -
In that function you must call the
run()
method on the first parameter passed to the function, using thecypher
andparams
variables. -
await
the results and useconsole.log
to check that the code has executed correctly. -
To add the new node and relationship to the database, click the Debug icon to the left of the window and run Writing Data Challenge task, or use the integrated terminal window to run the following command:
shRun The Challengets-node src/challenges/write/challenge.ts
-
Once the code has run, click Verify and we will check that the node has been added to the database.
Verifying the Test
Once you have executed the code, click the Verify button and we will check that the code has been executed successfully.
Hint
To pass this challenge you must run the Cypher statement in a write transaction using the session.executeWrite()
method.
Solution
Compare your code with the solution here
// Import the driver
import neo4j from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
async function main() {
// Create a Driver Instance
const driver = neo4j.driver(
NEO4J_URI,
neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
)
// Open a new Session
const session = driver.session()
try {
const cypher = `
MATCH (m:Movie {title: "Matrix, The"})
CREATE (p:Person {name: $name})
CREATE (p)-[:ACTED_IN]->(m)
RETURN p
`
const params = { name: 'Your Name' }
// Execute the `cypher` statement in a write transaction
const res = await session.executeWrite(
tx => tx.run(cypher, params)
)
console.log(res.records[0].get('p'))
}
finally {
// Close the session
await session.close()
}
}
main()
Lesson Summary
In this challenge, you used your knowledge to create a driver instance and run a Cypher statement.
Next, we will look at the Neo4j Type System and some of the considerations that you need to make when working with values coming from Neo4j in your TypeScript application.