psycopg2.ProgrammingError: the connection cannot be re-entered recursively

Viewed 35

Am calling an endpoint from flask using fetch api from react. I keep getting psycopg2.ProgrammingError: the connection cannot be re-entered recursively

I the endpoint call is inside a loop.

@app.get("/api/plot-project/<int:plot_id>/<int:project_id>")
def check_and_deactivate(plot_id, project_id):
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(PLOT_PROJECT_CHECK, (plot_id, project_id))
            data = cursor.fetchall()
            if len(data) == 0:
                return "No data found", 404
            removed = data.pop(0)
            if  len(data) > 1:
                for row in data:
                    print(row[0])
                    cursor.execute(PLOT_PROJECT_DEACTIVATE, ('deleted', row[0], plot_id, project_id))
            return { "Remain": removed }, 200   

The react fuctions

  const handleGet = () => {
    data.forEach (async (item) => {
      
      await getData(item.plotID, item.projectID);
    })
  }

the fetch handle

  const getData = async (plotID, projectID) => { 
    fetch(`http://127.0.0.1:5000/api/plot-project/${plotID}/${projectID}`, {  method : 'GET',  mode: 'no-cors', headers : {    'Content-Type': 'application/json',    'Authorization': `Bearer ${token}`  }})
      .then(data => data.json())
      .then((response) => {
        console.log('mapping', plotID, "to", projectID)
        console.log('request succeeded with JSON response', response)
      }).catch(function (error) {
        console.log('mapping', plotID, "to", projectID)
        console.log('no mapping yet')
      });
  }
0 Answers
Related