TypeError: 'NoneType' object is not iterable in python with sqlalchemy

1

I write because I can not solve a problem with a query made by my API that is made in Python. This same error does not affect the functioning of the API but I want to remove it anyway because it looks sloppy or clearly not programmed with SQLAlchemy or Flask.

in my @app.route I have this:

@app.route('/verificarincidencia/<idservice>', methods=['GET'])
@requires_auth
def verificarincidencia_get(idservice):
    if request.method == 'GET':
        checkincidencia = db.session.query(reclamo,reclamoservicioasignacionafectado,servicioasignacion).join(reclamo.reclamoservicioasignacionafectado, reclamoservicioasignacionafectado.servicioasignacion).filter(servicioasignacion.idservicioasignacion==reclamoservicioasignacionafectado.idservicioasignacion).filter(reclamo.idreclamo==reclamoservicioasignacionafectado.idreclamo).filter(reclamo.masivo==1).filter(reclamo.estado!='Solucionado').filter(servicioasignacion.idservicioasignacion==idservice).first()

return jsonify( {'reclamo': [elemento.as_dict() for elemento in checkincidencia] })

and after doing that verification and that return does not return anything in the console I get this error

return jsonify( {'reclamo': [elemento.as_dict() for elemento in checkincidencia] })
TypeError: 'NoneType' object is not iterable

I understand that if it does not return anything it should be understood that the object is empty and it is not iterable, but how can I solve it? try putting it like this:

if checkincidencia is not None:     
        return jsonify( {'reclamo': [elemento.as_dict() for elemento in checkincidencia] })
else:
    options = checkincidencia
    return jsonify(options)

but it's still the same just now it tells me that return jsonify(options) is not iterable.

Is my doubt understood? from now, thank you very much to the people of the community who have always helped me! Greetings!

    
asked by Francisco Puebla 07.12.2016 в 14:35
source

1 answer

0

Indeed, first returns None , which is not iterable, when the query returns no value.

A good way to solve it, and avoid checking each query, is to add an empty iterative in the list comprehension that will only be evaluated if the first element (the query) is None .

return jsonify(
    {'reclamo': [elemento.as_dict() for elemento in checkincidencia or []]}
)
    
answered by 07.12.2016 / 17:50
source