0
       templates: {
        suggestion: Handlebars.compile(
            "<div>" + 
            "{{place_name}}, {{admin_name1}}, {{postal_code}}" + 
            "</div>"
        )
    }

In my search function if I have the q value in brackets return jsonify([q]) the search box suggests only ",,". But if I remove the brackets return jsonify(q)the search box works perfectly. Why is that, and how should I edit the configure function if I wanted to use return jsonify([q])?

1 Answer 1

3

According to the spec [emphasis added]:

Complete the implementation of /search in such a way that it outputs a JSON array of objects, each of which represents a row from places that somehow matches the value of q, as in the staff solution below.

If you return jsonify([q]) it is returning a JSON array of a JSON array of objects. You shouldn't use that. It's guaranteed to cause undue heartache as you progress through the pset.

2
  • How could I have looked it up that return jsonify([q]) returns a JSON of a JSON?
    – Cristian
    Commented Jun 2, 2017 at 16:12
  • Not so much look it up as learn it. In python, anything enclosed in [ ] is a list. Assuming q is the result of a db.execute(SELECT), you know that q is a list of dict objects from pset7 spec hints. So this [q] creates a nested list. Technically [ ] is a list comprehension a powerful, fun and confounding! feature of python. It's not discussed in the course, but it is something you'll definitely want to learn and use if you progress in python. Commented Jun 2, 2017 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .