Index out of range while using PUT method on flask_mongoengine API

136 Views Asked by At

My code for the PUT method,

@app.route('/update_doc/<id>', methods=["PUT"])
def update_method(id):
    body = request.get_json()
    updated_document = db_collection.objects.get(id=id).update(**body)
    #document.modify(**body)
    return 'success', 200

I am getting the following error,

 Traceback (most recent call last):
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/configcar_it/Documents/k8s/ec2_mongodb/ec2_mongodb_validator.py", line 119, in update_method
    store = Store_collection.objects.get(id=id).update(**body)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/document.py", line 610, in update
    return self._qs.filter(**self._object_key).update_one(**kwargs)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/base.py", line 631, in update_one
    return self.update(
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/base.py", line 555, in update
    update = transform.update(queryset._document, **update)
  File "/home/configcar_it/anaconda3/envs/ec2_mongodb/lib/python3.9/site-packages/mongoengine/queryset/transform.py", line 301, in update
    field = cleaned_fields[-1]
IndexError: list index out of range

The remaining Post, Delete, Get methods are working. I am giving the input through the postman.

Delete method

@app.route('/stores/<id>', methods=['DELETE'])
def delete_doc(id):
    stores = Store_collection.objects.get(id=id).delete()
    return 'successfully deleted.', 200
1

There are 1 best solutions below

0
On BEST ANSWER

After browsing through the internet, I found out that I specified the field name as "type" in my MongoDB database. After understanding this, I changed "type" --> "types". Now it's working properly. I understood this by reading the following GitHub issue --> https://github.com/MongoEngine/mongoengine/issues/1194.

Also, the same issue is similar to the word "size". Hope this might be helpful for someone.