简体   繁体   English

Python:werkzeug.exceptions.InternalServerError:500 内部服务器错误:'NoneType' object 不可迭代

[英]Python: werkzeug.exceptions.InternalServerError: 500 Internal Server Error: 'NoneType' object is not iterable

I've seen many solutions about this problem but any of them solved mine.我已经看到了很多关于这个问题的解决方案,但其中任何一个都解决了我的问题。

I've this INSERT query which is wrapped in a try/except and, although it works fine in the database (the data is properly inserted), a Traceback is called, the code jumps to the except and the code below the query execution is just ignored (which cannnot happen in my case).我有这个INSERT查询,它包含在 try/except 中,虽然它在数据库中工作正常(数据已正确插入),但调用了 Traceback,代码跳转到 except,查询执行下面的代码是只是被忽略了(在我的情况下不会发生)。

It is something like this:它是这样的:

query = """
  INSERT INTO
    mytable(user_id, tcx_extension, user_email, created_date)
  VALUES
    {}
""".format("('12345678910', '6666', 'test123@test.com', '2020/01/06 00:00:00')")

self.pgsql.execute_query(query)  // the error is triggered here

//important code below that is ignored when the error fires

the self.pgsql.execute_query function is shown below: self.pgsql.execute_query function 如下所示:

def execute_query(self, query, retry=0, format=True):
    if self.pool is None:
        self._open_pool(PGSQL["connection_string"])

    try:
        conn = self.pool.getconn()

        cur = conn.cursor()
        conn.autocommit = True

        LOG.info("Executing query", extra={"query": query})
        cur.execute(query)

        if format is True:
            data = Formatter.format_cursor(cur)
        else:
            data = None
        self.pool.putconn(conn)
        cur.close()

   except pool.PoolError as e:
        LOG.error("PoolError: {}".format(e), extra={"query": query})
        if retry > MAX_RETRIES:
            raise TooManyRetries("Too many retries: {}".format(e))

        LOG.info("Will retry: {}".format(e), extra={"query": query})
        time.sleep(1)
        return self.execute_query(query, retry + 1)

    return data

The Traceback is shown below: Traceback 如下图所示:

[2021-01-06 15:28:15,421] ERROR in app: Exception on /members/manage_socket_members [POST]
Traceback (most recent call last):
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\flask_restful\__init__.py", line 480, in wrapper
    resp = resource(*args, **kwargs)
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\flask_restful\__init__.py", line 595, in dispatch_request
    resp = meth(*args, **kwargs)
  File "C:\Users\artur.santos\Documents\#salesforce\rc-analytics-api\src\decorators\auth.py", line 70, in wrap
    return f(*args, **kwargs)
  File "C:\Users\artur.santos\Documents\#salesforce\rc-analytics-api\src\members\manage_socket_members.py", line 30, in post
    return abort(500, error)
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\werkzeug\exceptions.py", line 707, in abort
    return _aborter(status, *args, **kwargs)
  File "C:\Users\artur.santos\AppData\Local\Programs\Python\Python37\lib\site-packages\werkzeug\exceptions.py", line 687, in __call__
    raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.InternalServerError: 500 Internal Server Error: 'NoneType' object is not iterable

Any ideas on how to solve this?关于如何解决这个问题的任何想法?

EDIT 1: All the table fields are of type 'VARCHAR' except for the created_date .编辑 1:除了created_date之外,所有表字段都是“VARCHAR”类型。

EDIT 2: When I try to insert some values between double quotes the error changes (which is also a nonsense error for me).编辑2:当我尝试在双引号之间插入一些值时,错误会发生变化(这对我来说也是一个无意义的错误)。 I don't know if gets me closer to the solution.我不知道是否让我更接近解决方案。 Check it out the logs I printed看看我打印的日志

> query: INSERT INTO wfm.socket_user_relation (user_id, tcx_extension, user_email, created_date) VALUES (12345678937, 63, "test37@test.com.br", "2021/01/07 09:25:35")
> exception: column "test37@test.com.br" does not exist
LINE 1: ...ser_email, created_date) VALUES (12345678937, 63, "test37@te...
                                                             ^

EDIT 3: If I try to INSERT all of them double quoted, the error remains the same: Error: 500 Internal Server Error: 'NoneType' object is not iterable编辑 3:如果我尝试INSERT所有双引号,错误保持不变: Error: 500 Internal Server Error: 'NoneType' object is not iterable

You are not setting the format parameter when executing the function and therefore it's always True (as you stablished in the function definition).您在执行 function 时没有设置format参数,因此它始终为True (正如您在 function 定义中所建立的那样)。

The response when you successfully insert something in the PgSQL DB is None and it's not possible to String format a NoneType Object .在 PgSQL DB 中成功插入某些内容时的响应是None并且不可能对NoneType Object进行字符串格式化。

Just use the code above and you will be fine.只需使用上面的代码,你会没事的。

self.pgsql.execute_query(query, format=False).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM