Jamie Munro
Jamie Munro

Reputation: 3

Parameter Missing: Expected parameter(s): Flask Python Py2neo

I am just getting started with Python, Flask and Neo4j. I am struggling with trying to pass a parameter from a url to a cypher query via py2neo.

I have tried a number of suggestions from the forums, tutorials and examples on github but have not managed to get this to work and was hoping someone could point me in the right direction.

This is views.py

#Single Comic
@app.route('/comic/<issue>/')
def comic(issue):

    # Get comic
    issue = comics.get_issue(issue)

    return render_template('issue.html', issue=issue)

This is my models.py (if I hard code the issue number into the query it shows correctly)

 class comics:
    def __init__(self, issue):
       self.issue = issue;

    def get_issue(issue):
        query = '''
        MATCH (c:Comic) WHERE c.Issue=$issue
        RETURN c.Issue as issue, c.Title as title
        '''
        return graph.run(query)

This is the issue.html template

{% extends "base.html" %}
{% block content %}
  {% for row in issue %}
        <h1>{{ row.issue }}: {{ row.title }} </h1>
        <img src="/static/comics/{{ row.issue }}.png" alt="{{ row.title }}">
        {% endfor %}

{% endblock %}

When I try to browse www.mydomain.com/comic/1 I get a "Parameter Missing: Expected parameter(s): issue" error

Many thanks

Upvotes: 0

Views: 746

Answers (1)

Nigel Small
Nigel Small

Reputation: 4495

You're almost there.

For Cypher substitution, you simply need to pass the parameter as a keyword argument into graph.run. So you'll end up with something like:

class comics:
    def __init__(self, issue):
       self.issue = issue;

    def get_issue(issue):
        query = '''
        MATCH (c:Comic) WHERE c.Issue=$issue
        RETURN c.Issue as issue, c.Title as title
        '''
        return graph.run(query, issue=issue)

It looks like you were expecting the variable to be absorbed from the enclosing scope, but this is a remote query wherein both statement template and parameters are transmitted over the wire. So the driver doesn't do the substitution, it happens on the server, within the Cypher engine. To that end, everything needs to be supplied explicitly.

Upvotes: 1

Related Questions