cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Single quote to double quotes

Hello,

I tried to send json to my nodeJS but when i load the result to my transaction there is an error.
First, this is the JSON that i send by POST request.


{
"allMenus":[
{"name":"FISH","color":"bg-light-pink"},
{"name":"SALAD","color":"bg-light-green"}
]
}


Then, this is my function:

const createMultiplesCategories=(req,res)=>{
const {allMenus}=req.body
console.log(allMenus)
const session = driver.session()
let status=0
session.writeTransaction(transaction=> {
return transaction.run(WITH ${allMenus} AS categories UNWIND categories AS categorie CREATE (n:Category {name: categorie.name, color: categorie.color}) );
})
.then(result => {
status=1;
})
.catch(error => {
console.log('error: ', error)
})
.finally(()=>{
session.close();
return res.json({ status});
})

}


Error console output(allMenus Json received + Error):

[ { name: 'FISH', color: 'bg-light-pink' },
{ name: 'SALAD', color: 'bg-light-green' } ]
error: { Neo4jError: Invalid input 'b': expected 'r/R' (line 2, column 42 (offset: 47))
" [object Object],[object Object]"


The error is due to the single quote
on Neo4j Browser INterface , this is correct

WITH [{ name: "HAMBURGER", color: "bg-washed-red" }] AS categories
UNWIND categories AS categorie
CREATE (n:Category {name: categorie.name, color: categorie.color})

Thanks,
(it's for my School project)

1 REPLY 1

Solved, my solution if it can help someone

const createMultipleProducts=(req,res)=>{
const {allMenus}=req.body
console.log(allMenus)
const session = driver.session()
let status=0
session.writeTransaction(transaction=> {
const cypher="WITH {allMenus} AS categories UNWIND categories AS category CREATE (n:Category) SET n.name = category.name , n.color=category.color";
const params={allMenus: allMenus}
return transaction.run(cypher,params);
})
.then(result => {
status=1;
})
.catch(error => {
console.log('error: ', error)
})
.finally(()=>{
session.close();
return res.json({ status});
})
}