I've been trying to call an API that I've created with Flask in python through Axios, but when I try logging my response data to console it returns undefined. When the API is called I get success messages in my Python file but my response is still undefined from my Axios call. The API also works when tested via browser and Postman. This is my code up to the Axios call that isn't working.
// load the things we need
var express = require('express');
var app = express();
const bodyParser = require('body-parser');
// required module to make calls to api
const axios = require('axios');
const { response } = require('express');
const { name } = require('ejs');
app.use(bodyParser.urlencoded());
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Home page for adding a user
app.get('/', function(req, res) {
res.render('pages/index')
});
app.post('/process_form', function(req, res) {
// Create variable to hold the first name from the request body
var firstname = req.body.firstname
// Create variable to hold the last name from the request body
var lastname = req.body.lastname
// Create variable to hold the first name from the request body
var favoriterestaurant = req.body.favoriterestaurant
// Log to console to create mental checkpoint
console.log("The user's name is " + firstname + " " + lastname + ", and their favorite restaurant is " + favoriterestaurant);
});
// Page for selecting attending users
app.get('/attendance', function(req, res) {
axios.get('http://127.0.0.1:5000/api/users')
.then((response)=>{
var users = response.data.results;
console.log(users);
});
// Load page for attendance
res.render('pages/attendance')
});
The first two routes work fine and the page with the Axios call still loads, I just can't seem to get a response. My API call also returns a JSON object if any are wondering.
When I switch the code around and drop the }); below res.render('/pages/attendance'), I get a 404 error in my Python code and a JSON response with a bunch of random stuff (starting with undefined) is returned. Been trying this for two days and I'm not sure what else I can do at this point.