Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-05-2019 08:41 AM
I'm trying to make a simple deleteRelationship in Javascript where it can take in a relationship id. Something simple like this-
MATCH (a)-[r]-(b) WHERE id(r)=$id DELETE r
Unfortunately, if I hardcode the $id, it works, but if I pass it in as a variable, I don't get a record. Both are number types, but it still seems to not be finding the relationship.
Solved! Go to Solution.
12-06-2019 01:59 AM
You all got me on the right track though. toInt worked for this part of my query. Looks like it was constantly changing it to floats if in JS you just use a basic number format.
const DELETE_RELATIONSHIPS = `
UNWIND $relationshipIds AS relationshipId
MATCH (a)-[r]-(b)
WHERE id(r)=toInt(relationshipId)
DELETE r
`;
12-05-2019 09:37 AM
You can use :param id => 12345
in browser to set a parameter.
12-05-2019 02:15 PM
Is the ID parameter converted to a neo4j int? neo4j.int(100)
12-05-2019 02:58 PM
Hi Thomas,
You can use an Array like this.
:param id => [7,3,2]
MATCH (a)-[r]->(b)
WHERE id(r) IN $id
DELETE r
12-06-2019 01:17 AM
Sorry, I was saying that if you pass it in using a JS function. Shouldn't have mentioned being able to hardcode it in the browser. Here is the long version that won't find the relationship ID that clearly exists in the browser and works if I hardcode it.
huum.deleteRelationships([3215630])
const deleteRelationships = relationshipIds => {
return new Promise(async function(resolve, reject) {
try {
const idsToDelete = { relationshipIds: relationshipIds };
let queryResult = await runQuery(DELETE_RELATIONSHIPS, idsToDelete);
resolve(queryResult);
} catch (err) {
reject(err);
}
});
};
const runQuery = (query, params) => {
return new Promise(function(resolve, reject) {
const session = driver.session();
session
.run(query, params)
.then(function(result) {
console.log("Success ", query);
session.close();
driver.close();
resolve(result);
})
.catch(function(error) {
console.log("Error running query!", error);
session.close();
driver.close();
reject(error);
});
});
};
const DELETE_RELATIONSHIPS = `
UNWIND $relationshipIds AS relationshipId
MATCH (a)-[r]-(b)
WHERE id(r)=relationshipId
DELETE r
`;
12-06-2019 01:59 AM
You all got me on the right track though. toInt worked for this part of my query. Looks like it was constantly changing it to floats if in JS you just use a basic number format.
const DELETE_RELATIONSHIPS = `
UNWIND $relationshipIds AS relationshipId
MATCH (a)-[r]-(b)
WHERE id(r)=toInt(relationshipId)
DELETE r
`;
12-06-2019 12:22 PM
Hi Nick,
JS only has numbers (with a decimal), so if you pass in something that looks like an Int, you need to convert it as you have shown. This isn't Neo4j behavior per se, it's js converting it to a float while you weren't looking , then it no longer matches.
David
12-06-2019 02:46 AM
I would convert it to a "neo4j int" in JS using neo4j.int(integer).
All the sessions of the conference are now available online