'Problem in uploading data to neo4j through dart

I had to upload data to neo4j and I am using dart for this. Everything was fine until I added some variables.

Since the data is large I had to go through a loop and this is where the problems started.

This is the code:

for (int i = 0; i < data[0]['Link'].length - 1; i++) {
  http.Response res = await http.post(
    uri,
    body: json.encode({
      "statements": [
        {
          "statement": "MATCH (y:Year {value: '5711'}) CREATE (m:Month {name: '${months[i]}'})-[:Month_of]->(y) CREATE (e:Event {content: '${json.encode(eventContent[months[i]])}'}) -[:Event_of {date: '${json.encode(dates[i])}', link:'${json.encode(events[i][0]['link'])}'}]->(m) return y, m, e",
          "parameters": {
            "year": year,
            "month": months[i],
            "date": dates[i],
            "event": eventContent[months[i]],
            "link": events[i][0]['link']
          },
        },
      ]
    }),
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json"
    },
  );
  response.add(res);
}

Not sure where the error starts, I went through the docs but they have written nothing about queries from dart.



Solution 1:[1]

You are mixing query parameterization (handled later by the server-side Cypher runtime) and string interpolation (done on the client side right away).

You should try something like this:

      {
        "statement": "MATCH (y:Year {value: $year}) CREATE (m:Month {name: $month})-[:Month_of]->(y) CREATE (e:Event {content: $event}) -[:Event_of {date: $date, link:$link}]->(m) return y, m, e",
        "parameters": {
          "year": year,
          "month": months[i],
          "date": json.encode(dates[i]),
          "event": eventContent[months[i]],
          "link": events[i][0]['link']
        },
      },

Query parameter cannot be surrounded by quotes and simply consist of $ followed by the corresponding parameter name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fbiville