Reputation: 302
Hi how can I set Label in query;
exports.addNewLabel = function(req, res) {
session
.run("MATCH (n:User {id:{userId}}) SET n:{newLabel} RETURN n", {
userId: req.params.userId,
newLabel: req.body.newLabel
})
.then(function(result) {
res.status(200).send({ succeed: true, data: result.records[0]._fields });
session.close();
})
.catch(function(err) {
console.log(err);
res.status(404).send({ succeed: false, data: err });
});
};
my post api query:
rest/user/addnewlabel/5a423325507c093948e9ef91
body :
{
"newLabel":"Firm"
}
when I call my api for create new label I take this mistake as below code. when I set relationships as like also I get this error.
Neo4jError: Invalid input '{': expected whitespace or a label name (line 1,
column 36 (offset: 35))
"MATCH (n:User {id:{userId}}) SET n:{newLabel} RETURN n"
Upvotes: 0
Views: 353
Reputation: 29147
I think that a simpler, safer and more elegant way is to use the addLabels
procedure from the APOC library
:
session
.run(
`
MATCH (n:User {id: {userId}})
CALL apoc.create.addLabels(n, {newLabels}) YIELD node
RETURN node AS n
`, {
userId: req.params.userId,
newLabels: [req.body.newLabel]
})
Upvotes: 0
Reputation: 302
I found new solution also I can write a query.
exports.addNewLabel = function(req, res) {
var query ="MATCH (n:User {id:'"+req.params.userId+"'}) SET n:"+req.body.newLabel+" RETURN n"
session
.run(query)
.then(function(result) {
res.status(200).send({ succeed: true, data: result });
session.close();
})
.catch(function(err) {
console.log(err);
res.status(404).send({ succeed: false, data: err });
});
};
Upvotes: 0
Reputation: 20185
You cannot set labels dynamically via parameters, the same is true for relationship types as well.
You can use string concatenation instead :
session
.run("MATCH (n:User {id:{userId}}) SET n:`" + req.body.newLabel + "` RETURN n", {
userId: req.params.userId
})
I would suggest to validate the request though.
Upvotes: 1