0

I'm having troubles sending my form data to MongoDB with a Flask setup. The form looks something like this: https://jsfiddle.net/8gqLtv7e

On the client-side, I see no errors. But when I submit the form, I receive a 500 Internal Server Error and I'm having a hard time finding the solution. The problem is the last line below in my views.py file:

@app.route('/recordReport', methods=['POST'])
def recordReport():
    homeReportsCollection = db["reports"]
    address=request.form.get['address']
    rgbImage=request.form.get['rgb']
    homeReportsCollection.insert({'address':address, 'rgb':rgbImage})

Because if I replace that with return json.dumps({'status':'OK', 'address':'address', 'rgb':'rgbImage'}), I can see the correct data in my browser. Am just not able to send it to a collection in MongoDB.

kaoscify
  • 1,743
  • 7
  • 37
  • 74
  • Shouldn't you be using `insert_one()`? – Quirk Dec 16 '15 at 20:47
  • `homeReportsCollection.insert_one({'address':address, 'rgb':rgbImage})` still leads to a 500 Internal Server Error. – kaoscify Dec 16 '15 at 20:50
  • Have you tried type casting `address` and `rgbImage` to `String` before inserting? – Quirk Dec 16 '15 at 20:58
  • I made a typo. Both `address` and `rgbImage` are sent as strings. Just edited my question. So no need to do it on the Python side. – kaoscify Dec 16 '15 at 21:11
  • 1
    I'll be honest, I'm stumped. There used to be a bug in Mongo back in 2013. The data would be inserted into the collection. But Mongo would not return a correct status response. That led to servers going 500. Have you tried verifying if the data was indeed inserted into the collection? Additionally run your flask app in `debug=True` mode. That might give additional data. – Quirk Dec 16 '15 at 21:24
  • Do take a look into http://stackoverflow.com/questions/32993220/inserting-or-saving-into-mongodb-returns-500-server-error . However Py-Mongo should be able to handle this. – Quirk Dec 16 '15 at 21:28
  • So this is weird, I turned on `debug=True` and I get the following error: `ValueError: View function did not return a response`. BUT the data did actually get sent to DB via `homeReportsCollection.insert({'address':address, 'rgb':rgbImage})` line. I see it in my collection. How do I fix the error? Because the user is redirected to `/recordReport`. – kaoscify Dec 16 '15 at 21:39
  • Try returning something in your function `recordReport()`. Return anything you want. It could be an `OK` message. It could be the `_id` of the document you just inserted into the collection. Just don't return `None`. Try this. – Quirk Dec 17 '15 at 13:00
  • Yeah, I returned an HTML template and no error now. Thanks for your help. I think it's ok for now. Do you want to post this as answer so I can reward you points? – kaoscify Dec 18 '15 at 17:15
  • Added an answer summarizing the steps and solution. ;-) – Quirk Dec 20 '15 at 13:50

1 Answers1

1

This answer is a summary of the comments (where a solution was found).


Have you tried typecasting address and rgbImage to String before inserting?

Type invalidation is the root of many common bugs in DB operations.

There used to be a bug in Mongo back in 2013. The data would be inserted into the collection. But Mongo would not return a correct status response. That led to servers going 500. Have you tried verifying if the data was indeed inserted into the collection?

Additionally run your flask app in debug=True mode. That might give additional data.

Flask has very good debug traceback reporting support. This is generally a good idea. In fact this should be the first thing to do when encountering an error.

So this is weird, I turned on debug=True and I get the following error: ValueError: View function did not return a response. BUT the data did actually get sent to DB via homeReportsCollection.insert({'address':address, 'rgb':rgbImage}) line. I see it in my collection. How do I fix the error? Because the user is redirected to /recordReport.

So the data was indeed inserted into the collection. It is possibly a Flask only issue. The traceback says it all. Flask requires that something is returned by a view method.

Try returning something in your function recordReport(). Return anything you want. It could be an OK message. It could be the _id of the document you just inserted into the collection. Just don't return None. Try this.

This behaviour is documented in this SO question.

Yeah, I returned an HTML template and no error now.

This is indeed the solution. Return something other than None from your view methods. This also validates the behaviour observed by asker in the question:

Because if I replace that with return json.dumps({'status':'OK', 'address':'address', 'rgb':'rgbImage'}), I can see the correct data in my browser.

Community
  • 1
  • 1
Quirk
  • 1,305
  • 4
  • 15
  • 29