I am new to Neo4J and trying to check if a User node FOLLOWS another user node in my graph database and return a boolean if the relationship exists between the two nodes.
In my current query, I match the two users and then use exists() to see if the FOLLOW relationship exists between them but I got the error:
PatternExpressions are not allowed to introduce new variables: 'f'.
So I modified the query to below to introduce the f:FOLLOWS relationship to check if it exists for RETURN but instead of returning a boolean it returned an object and NOT a boolean. When I logged the return result to the console it says [object Object] and the keys are [ 'records', 'summary' ]
MATCH (u1:User{userId: $reqUserId}), (u2:User {userId: $userId})
MATCH (u1)-[f:FOLLOWS]->(u2)
RETURN exists((u1)-[c]->(u2))
What am I doing wrong?
const checkIfUserCollectsUser = async (reqUserId, userId) => {
let driver = neo4j.driver(
process.env.NEO4J_URL,
neo4j.auth.basic(process.env.NEO4J_USERNAME, process.env.NEO4J_PASSWORD)
)
let session = driver.session()
return new Promise(async (resolve, reject) => {
try {
const readQuery = `
MATCH (u1:User{userId: $reqUserId}), (u2:User {userId: $userId})
RETURN exists((u1)-[f:FOLLOWS]->(u2))
`
const bool = await session.executeRead((tx) =>
tx.run(readQuery, { reqUserId: reqUserId, userId: userId })
)
resolve(bool)
} catch (err) {
console.log(err.message)
reject({ message: err.messages })
} finally {
await session.close()
}
await driver.close()
})
}
CodePudding user response:
Your pattern (u1)-[f:FOLLOWS]->(u2) does not need a new variable f. It is not usable elsewhere. You simply remove it and it should work.
For example:
MATCH (u1:User{userId: $reqUserId}), (u2:User {userId: $userId})
RETURN exists((u1)-[:FOLLOWS]->(u2))
Result:
╒═════════════════════════════╕
│"exists((u1)-[:LINKS]->(u2))"│
╞═════════════════════════════╡
│true │
└─────────────────────────────┘