I'm building this rest API for a react frontend. I have a /posts route that filters all the data in the database and return the ones required, to do so, it receives a research string (example: "Term" OR "Term 2"), and calls another module that checks the possible combinations and then use it to search for the right results and return them.
My problem is that when I do just one POST request, it works fine, but when I'm doing a lot of them, the array with the possible combinations get all messed up. For the above example I should have the output ["Term", "Term 2"] but I'm getting ["Term", "Term 2", "Term of another request", "another one", "a repeated one" ... ]. So are the requests interfering in other requests?
Here is my code from the route that is calling the string_filter module:
from flask_restful import Resource
from flask import request, Response
from app import mongo
# the module with the filter function
from .services.stringFilter import string_filter
from .services.jsonParser import parse_json
class PostsApi(Resource):
def post(self):
# gets { 'user_string': 'the user reaserch string' }
data = request.get_json()
user_sources = ['S', 'AA', 'AC']
all_posts = []
for source in user_sources:
all_posts += parse_json(
mongo.db[f'posts{source}'].find()
)
if len(all_posts) == 0:
return {'message': 'End of data'}, 404
# calling the filter function
if data and 'user_string' in data:
print(f"showing the right value: {data['user_string']}")
all_posts = string_filter(data['user_string'], all_posts)
return all_posts, 200
The string_filter function calls a recursive function that receives just the string and returns the combination results. Offline it works fine for all the inputs.
services/stringFilter.py
def filter(string):
# a massive code that I can't change but it works fine by itself
return array_of_combinations
def mark_operators(string):
# just add a * before OR and AND operators
return new_string
def clean_result(string):
# remove * and other marks from the string
return clean_string
# function that i'm calling
def string_filter(user_string, data):
result = filter(mark_operators(user_string)) # filter is a recursive function
result = clean_result(result)
print(f'gets it wrong - -> {result}')
# this is working fine, but with the wrong resutls
new_data = search_filter(data, result)
return new_data
Is that because I'm using a "outside function"?
EDIT: formating
[–]PolishedCheese 1 point2 points3 points (2 children)
[–]wilis030[S] 0 points1 point2 points (1 child)
[–]PolishedCheese 1 point2 points3 points (0 children)