cancel
Showing results for 
Search instead for 
Did you mean: 

merge multiple node with neo4j-driver in js

hello 

im trying to get variables of an array and create node for them so i have a foreach

words.forEach((res, index) => {

to get all of the items and then run this query 

session.run(`MERGE (n:words{name:'${res}'})`) }
but i get this error
Queries cannot be run directly on a session with an open transaction; either run from within the transaction or use a different session.
 
i tried to use rxsession but didnt work
1 ACCEPTED SOLUTION

- use real parameters not string substitution
- if you want to run multiple operations, use session.writeTransaction() and tx.run()
- but the real solution here is to pass in your list of words as a parameter and use UNWIND

UNWIND $words AS word
MERGE (:Word {name:word})

 

View solution in original post

6 REPLIES 6

- use real parameters not string substitution
- if you want to run multiple operations, use session.writeTransaction() and tx.run()
- but the real solution here is to pass in your list of words as a parameter and use UNWIND

UNWIND $words AS word
MERGE (:Word {name:word})

 

hi @michael_hunger 

my word list is like this [He,says,that,with,my,imaginative,power,and,habit,of,story-making,a,nervous,weakness,like,mine,is,sure,to,lead,to,all,manner,of,excited,fancies,and,that,I,ought,to,use,my,will,and,good,sense,to,check,the,tendency,.]

when i try to use query with unwind it wont insert any thing into database

my code is 

            session.run(`unwind ${words} as word merge (:words{name : word})`)
only way this insert is when i put ${words} into quotation and by this query it just insert all of this list into 1 node

hi @michael_hunger 

i tried what u said but didn't work . my list is like this [He,says,that,with,my,imaginative,power,and,habit,of,story-making,a,nervous,weakness,like,mine,is,sure,to,lead,to,all,manner,of,excited,fancies,and,that,I,ought,to,use,my,will,and,good,sense,to,check,the,tendency,.]

my query is this :

session.run("unwind $word as item merge (:words{name : item})"),{
                word:words
            }
the only way it worked was put $word in to quotation and it just make all of the list as 1 node

What does "didn't work" mean?
I can't see anything (except database connection credentials being wrong) as a reason for this not to work.

 

 

look my connection its ok cause i test some queries it was fine

here is my list that im testing on

var n=['qut','test','ted','vgf']
and this is query im sending
session.run(`unwind ${n} as word MERGE (:words{name:word})`)
but nothing added to my database 

i fixed it it was because of node js

as i said my list was something like this ["test","test1"] but in node js when u wanna use that list it is like this [test,test1] so i add " " to them and then unwind them and it fixed

tnx a lot sir

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.