validate_insert, status 200 and data="iderrors"

72 views
Skip to first unread message

Tito Garrido

unread,
Jul 17, 2019, 10:36:40 AM7/17/19
to web...@googlegroups.com
Hi!

I am trying to call a POST method from a Vue.js frontend. My backend is web2py serving a REST API:

@request.restful()
@cors_allow
def save_transaction():
    response.view = 'generic.json'

    def POST(**data):
        # Creating the parent transaction

        parent = {'parent_transaction' : None,
                  'transaction_date': '01/01/2019',
                  'transaction_label' : 'label',
                  'transaction_type' : data['transaction_type'],
                  'treatment': data['selected_treatment'],
                  'transaction_value': data['total_value'],
                  'paid': data['allpaid'],
                  'payment_method': None
                  }

        return db.transactions.validate_and_insert(**parent)

    return locals()

My DB is:

db.define_table('transactions',
                Field('parent_transaction', type='reference transactions', requires=IS_EMPTY_OR(IS_IN_DB(db, 'transactions.id', '%(transaction_label)s'))),
                Field('transaction_date', 'date', requires=IS_DATE(format='%d/%m/%Y',error_message='Coloque uma data no formato DD/MM/AAAA')),
                Field('transaction_label', 'string'),
                Field('transaction_type', 'string', requires=IS_IN_SET(['Receita','Despesa'])),
                Field('treatment', type='reference contact_treatment', requires=IS_EMPTY_OR(IS_IN_DB(db, 'contact_treatment.id', '%(description)s'))),
                Field('description', 'string', requires=IS_NOT_EMPTY(error_message='Preencha com uma descrição')),
                Field('transaction_by', 'reference contacts', requires=IS_EMPTY_OR(IS_IN_DB(db, 'contacts.id', '%(name)s',))),
                Field('transaction_value', 'float', requires=IS_FLOAT_IN_RANGE(None, None, dot=","), default=0),
                Field('paid', 'boolean', default=False),
                Field('payment_method', type='reference payment_methods', requires=IS_EMPTY_OR(IS_IN_DB(db, 'payment_methods.id', '%(name)s'))),
                )

This is the Data sent:
{'parent_transaction': None, 'transaction_date': '01/01/2019', 'transaction_label': 'label', 'transaction_type': 'Receita', 'treatment': 1, 'transaction_value': 250, 'paid': False, 'payment_method': None}

This is the response:

  1. config{url"http://127.0.0.1:8000/controle7030/product/save_transaction"method"post"data"{"total_value":250,"costs":[[{"id":1,"product":1,"…85","total_cost":66,"transaction_type":"Receita"}"headers{…}transformRequestArray(1), …}
  2. data"iderrors"
  3. headers{pragma"no-cache"content-type"text/html; charset=utf-8"cache-control"no-store, no-cache, must-revalidate, post-check=0, pre-check=0"expires"Wed, 17 Jul 2019 14:35:41 GMT"}
  4. requestXMLHttpRequest {onreadystatechangeƒreadyState4timeout0withCredentialsfalseuploadXMLHttpRequestUpload, …}
  5. status200
  6. statusText"OK"
  7. __proto__Object

Any idea what is wrong? What does mean "iderrors"?

Thank you!

Tito
--

Linux User #387870
.........____
.... _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:_______

Dave S

unread,
Jul 17, 2019, 1:23:10 PM7/17/19
to web2py-users


On Wednesday, July 17, 2019 at 7:36:40 AM UTC-7, Tito Garrido wrote:
Hi!

I am trying to call a POST method from a Vue.js frontend. My backend is web2py serving a REST API:
[...] 
This is the Data sent:
{'parent_transaction': None, 'transaction_date': '01/01/2019', 'transaction_label': 'label', 'transaction_type': 'Receita', 'treatment': 1, 'transaction_value': 250, 'paid': False, 'payment_method': None}

This is the response:

  1. config{url"http://127.0.0.1:8000/controle7030/product/save_transaction"method"post"data"{"total_value":250,"costs":[[{"id":1,"product":1,"…85","total_cost":66,"transaction_type":"Receita"}"headers{…}transformRequestArray(1), …}
  2. data"iderrors"
  3. headers{pragma"no-cache"content-type"text/html; charset=utf-8"cache-control"no-store, no-cache, must-revalidate, post-check=0, pre-check=0"expires"Wed, 17 Jul 2019 14:35:41 GMT"}
  4. requestXMLHttpRequest {onreadystatechangeƒreadyState4timeout0withCredentialsfalseuploadXMLHttpRequestUpload, …}
  5. status200
  6. statusText"OK"
  7. __proto__Object

Any idea what is wrong? What does mean "iderrors"?

Does that transaction get entered into the database? 

The one thought I have about "iderrors" is that you have a mishmash occurring in converting the result into JSON.

I'd expect the body of the response to look something like
{"id": 2345, "errors": None}

where 2345 is the value returned by validate_and_insert() [the id field of the new row in the table] and None to indicate no errors.

/dps


Tito Garrido

unread,
Jul 17, 2019, 2:02:59 PM7/17/19
to web...@googlegroups.com
No entry in the database... 

I could execute the post from postman... but if I try from axios in vue I am even getting a web2py server error:

ERROR:Rocket.Errors.Thread-2:Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1797, in run_app
    sections = len(output)
TypeError: object of type 'Reference' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1288, in run
    self.run_app(conn)
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1819, in run_app
    output.close()
TypeError: 'NoneType' object is not callable


No idea what it means...

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/daa835f7-e232-494c-9bbc-def2a249869f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
Jul 17, 2019, 6:40:07 PM7/17/19
to web2py-users

On Wednesday, July 17, 2019 at 11:02:59 AM UTC-7, Tito Garrido wrote:
No entry in the database... 

I could execute the post from postman... but if I try from axios in vue I am even getting a web2py server error:

ERROR:Rocket.Errors.Thread-2:Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1797, in run_app
    sections = len(output)
TypeError: object of type 'Reference' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1288, in run
    self.run_app(conn)
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1819, in run_app
    output.close()
TypeError: 'NoneType' object is not callable


No idea what it means...

Which version of web2py are you using?   Is this with Python2 or Python3?

In 2.18.2,
line 1797 of rocket.py immediately follows a check "if hasattr(output, '__len__'):"
which makes the exception surprising.

The additonal exception is perhaps of the "after the car landed in the river, the steering wheel did not turn the car" type.

/dps

Dave S

unread,
Jul 17, 2019, 10:51:59 PM7/17/19
to web2py-users


On Wednesday, July 17, 2019 at 3:40:07 PM UTC-7, Dave S wrote:

On Wednesday, July 17, 2019 at 11:02:59 AM UTC-7, Tito Garrido wrote:
No entry in the database... 

I could execute the post from postman... but if I try from axios in vue I am even getting a web2py server error:

ERROR:Rocket.Errors.Thread-2:Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1797, in run_app
    sections = len(output)
TypeError: object of type 'Reference' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1288, in run
    self.run_app(conn)
  File "/Users/titogarridoogando/Documents/Projetos/web2py/controle/web2py/gluon/rocket.py", line 1819, in run_app
    output.close()
TypeError: 'NoneType' object is not callable


No idea what it means...

Which version of web2py are you using?   Is this with Python2 or Python3?
 

Also, can you show us request.body, either from inside your controller, or from the browser tools?

If you do any logging, can you check what the return value of validate_and_insert() is?

(I use curl, or occasionally wget, for some request debugging; I'm not familiar with postman or axios; the one browser extension I've used is on the home computer, so I'll report its name later.)

/dps
 

Tito Garrido

unread,
Jul 18, 2019, 1:54:38 PM7/18/19
to web...@googlegroups.com
I just figured out that the error:

is because of:
return db.transactions.validate_and_insert(**parent)

seems that db.transactions.validate_and_insert(**parent) is causing an error during the return.. if I return a string it works...

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
Jul 18, 2019, 4:46:38 PM7/18/19
to web2py-users


On Thursday, July 18, 2019 at 10:54:38 AM UTC-7, Tito Garrido wrote:
I just figured out that the error:

is because of:
return db.transactions.validate_and_insert(**parent)

seems that db.transactions.validate_and_insert(**parent) is causing an error during the return.. if I return a string it works...

But you said there was no entry made in the DB, so validate_and_insert(**parent) must have a processing error, as well as not returning good stuff.   Use logging or print debug info to console to find out more.

/dps


To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages