Expected to find a node at ref slot 7 but found Map{ref -> (172)} instead

I have an interesting error

I am creating

(A)<-(Main Node)->(link)->(value or reference)

When I execute this query:

UNWIND $links AS linkData 
UNWIND COALESCE(linkData.references, []) AS refData     
    CALL apoc.cypher.run('MATCH (ref:`' + refData.label + '` {code: theCode}) RETURN ref', { theCode: refData.code })
    YIELD value AS r
RETURN r

I get the right result (the references), but when I execute it as part of the entire iteration:

// several other queries (some with optional matches, all works fine)
            WITH A, l, linkData
            CALL {      
                WITH A, l, linkData
                UNWIND COALESCE(linkData.references, []) AS refData
                CALL apoc.cypher.run('MATCH (ref:`' + refData.label + '` {code: theCode}) RETURN ref', { theCode: refData.code })
                YIELD value AS r
                MERGE (l)-[valu:value]->(r)
                ON CREATE
                    SET valu.created = timestamp(),
                        valu.updated = timestamp()
                ON MATCH
                    SET valu.updated = timestamp()            
            }

I get the error from the subject.

:params {
  "node": {
    "id": "123",
    "properties": {
      "name": "Main Node",
      "category": "CategoryA",
      "createdAt": "2025-03-10"
    },
    "parentId": "54:6b65977c-9dd8-4989-80f4-225b96258645:12750"
  },
  "links": [
    {
      "id": "link1",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "timeRange"
      },
      "values": [
        {
          "content": "ValueA1",
          "properties": {
            "format": "text",
            "size": 1024
          }
        },
        {
          "content": "ValueA2",
          "properties": {
            "format": "xml",
            "size": 2048
          }
        }
      ],
      "references": []
    },
    {
      "id": "link5",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "isAccessServiceOf"
      },
      "values": [
        {
          "content": "ValueB1",
          "properties": {
            "format": "jpg",
            "size": 1024
          }
        },
        {
          "content": "ValueB2",
          "properties": {
            "format": "rdf",
            "size": 2048
          }
        }
      ]
    },
    {
      "id": "link6",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "theme"
      },
      "references": [ 
        { 
          "label": "type_iso3166",
          "code" : "AF"
        },
        { 
          "label": "type_iso3166",
          "code" : "NA"
        }      
      ]
    }
  ]
}

@joshcornejo

any details specific to what Neo4j version this is encountered upon?

Version = 5.18.1

I'm waiting for the n10s release to upgrade (I use it for some other things)

Just to check I've just created a duplicate and restored to 5.27.0 ... still get the same error (now in a different pair of integers):

Expected to find a node at ref slot 2 but found Map{ref -> (15042)} instead

@joshcornejo

anything in logs/debug.log at time of failure, and specifically a stack trace?

I did try to reproduce and pasted

:params {
  "node": {
    "id": "123",
    "properties": {
      "name": "Main Node",
      "category": "CategoryA",
      "createdAt": "2025-03-10"
    },
    "parentId": "54:6b65977c-9dd8-4989-80f4-225b96258645:12750"
  },
  "links": [
    {
      "id": "link1",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "timeRange"
      },
      "values": [
        {
          "content": "ValueA1",
          "properties": {
            "format": "text",
            "size": 1024
          }
        },
        {
          "content": "ValueA2",
          "properties": {
            "format": "xml",
            "size": 2048
          }
        }
      ],
      "references": []
    },
    {
      "id": "link5",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "isAccessServiceOf"
      },
      "values": [
        {
          "content": "ValueB1",
          "properties": {
            "format": "jpg",
            "size": 1024
          }
        },
        {
          "content": "ValueB2",
          "properties": {
            "format": "rdf",
            "size": 2048
          }
        }
      ]
    },
    {
      "id": "link6",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "theme"
      },
      "references": [ 
        { 
          "label": "type_iso3166",
          "code" : "AF"
        },
        { 
          "label": "type_iso3166",
          "code" : "NA"
        }      
      ]
    }
  ]
}

into cypher-shell and it failed with

}: Invalid input '"node"': expected an identifier or '}' (line 2, column 3 (offset: 11))
"  "node": {"

I'll see if i can figure out

Nothing in the logs ...

just in case, here it is again. I get no problem, just:

Successfully set your parameters

:params {
  "node": {
    "id": "123",
    "properties": {
      "name": "Main Node",
      "category": "CategoryA",
      "createdAt": "2025-03-10"
    },
    "parentId": "54:6b65977c-9dd8-4989-80f4-225b96258645:12750"
  },
  "links": [
    {
      "id": "link1",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "timeRange"
      },
      "values": [
        {
          "content": "ValueA1",
          "properties": {
            "format": "text",
            "size": 1024
          }
        },
        {
          "content": "ValueA2",
          "properties": {
            "format": "xml",
            "size": 2048
          }
        }
      ]
    },
    {
      "id": "link5",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "isAccessServiceOf"
      },
      "values": [
        {
          "content": "ValueB1",
          "properties": {
            "format": "jpg",
            "size": 1024
          }
        },
        {
          "content": "ValueB2",
          "properties": {
            "format": "rdf",
            "size": 2048
          }
        }
      ]
    },
    {
      "id": "link6",
      "properties": {
        "createdAt": "2025-03-10",
        "status": "active"
      },
      "type": {
        "name": "theme"
      },
      "references": [ 
        { 
          "label": "type_iso3166",
          "code" : "AF"
        },
        { 
          "label": "type_iso3166",
          "code" : "NA"
        }      
      ]
    }
  ]
}

@dana_canzano

... this one was a cryptic one as the error message is sort of lost as where the problem is and I assume in a simpler query the returned value is interpreted by the browser in a different way from when you send it as part of a more complex query, but found out what was the problem:

apoc.cypher.run() returns a list of maps and the merge needs a node.

To return a node to the next part of the query, you have to UNWIND:

CALL apoc.cypher.run('MATCH (referee:`' + refData.label + '` {code: theCode}) RETURN referee', { theCode: refData.code })
  YIELD value 
  UNWIND value.referee AS r

my section of the query now looks:

           WITH A, l, linkData
           CALL {      
                WITH A, l, linkData
                UNWIND COALESCE(linkData.references, []) AS refData
                CALL apoc.cypher.run('MATCH (referee:`' + refData.label + '` {code: theCode}) RETURN referee', { theCode: refData.code })
                YIELD value 
                UNWIND value.referee AS r
                MERGE (l)-[valu:value]->(r)
                ON CREATE
                    SET valu.created = timestamp(),
                        valu.updated = timestamp()
                ON MATCH
                    SET valu.updated = timestamp()            
            }