ARTICLE AD BOX
I am trying to build an app with my own API. The API is created with Python using Flask. My API runs on local port 5000 and my IPv4 address is 192.168.0.195.
The API service code looks like this:
app = Flask(__name__) CORS(app) api = Api(app) basedir = os.path.abspath(os.path.dirname(__file__)) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join( basedir, "instance", "database.db" ) db = SQLAlchemy(app) """ API CODE BLAH BLAH """ api.add_resource(Lincenses, "/licenses/") api.add_resource(Subjects, "/subjects/<string:selected_license>/") api.add_resource( QuestionBank, "/questionbank/<string:selected_license>/<string:selected_subject>/" ) api.add_resource(GenerateQuizData, "/generatequizdata/") @app.route("/") def home(): return "api.py" if __name__ == "__main__": with app.app_context(): db.create_all() app.run(host="0.0.0.0", port=5000, debug=True)I make an API request with this code:
const [availableLicenses, setAvailableLicenses] = useState(null); const [selectedLicense, setSelectedLicense] = useState("none"); useEffect(() => { async function fetchAvailableLicenses() { try { const response = await fetch("http://192.168.0.195:5000/licenses/"); const availableLicenses = await response.json(); console.log(availableLicenses); setAvailableLicenses(availableLicenses.Data); } catch (error) { console.error("Fetch error:", error); } } fetchAvailableLicenses(); }, []);The fetch doesn't seem able to reach my API, since there isn't any request made in the API server, but the link is in fact the correct one and I can even use it in the browser of both my phone and laptop and it works fine. I am completely clueless on what is happening, this very same code used to work before, with other IPv4 addresses. I'm using Expo Go, though I don't think it is relevant.
The console.error("Fetch error:", error) just outputs: ERROR Fetch error: [TypeError: Network request failed] .
I ask Gemini what to do to solve it and it told me to add flask-cors to my API and use CORS(app) as shown above, but it did nothing.
Also, when i ran npx expo start it gave me this warning:
The following packages should be updated for best compatibility with the installed expo version: [email protected] - expected version: ~55.0.15 [email protected] - expected version: ~55.0.14 [email protected] - expected version: ~55.0.6 [email protected] - expected version: ~55.0.13 [email protected] - expected version: ~55.0.12 [email protected] - expected version: ~55.0.5 [email protected] - expected version: 0.83.4 Your project may not work correctly until you install the expected versions of the packages.But i don't think it has anything do to. And if it does, I don't know how to fix it.
If some one can spot the problem and help me out i'll be so thankful. i am fully stuck and can't continue in the project.
