Error fetching user data: Cannot read properties of undefined (reading ‘request’)

Problem:

I’m trying to fetch user data from Auth0 using their Management API in my Express app, but I’m encountering the following error:

Error fetching user data: Cannot read properties of undefined (reading ‘request’) Failed to update user metadata: Error: Failed to fetch user data from Auth0

This error occurs when making the Axios request to the /users-by-email Auth0 API endpoint. I’m not sure what might be causing the issue since I’ve double-checked the configuration. This request is called on this url: http://localhost:3000/fetch-user/[email protected]

the error is printed out here:

console.error('Error fetching user data:', error.response ? error.response.data : error.message);

and is most likley happening here:

const response = await axios.request(config);

Code:

Here is the relevant part of my Express server:

const express = require('express');
const axios = require('axios');
require('dotenv').config();

const app = express();

// Middleware to parse JSON request body
app.use(express.json());

// Function to get user data from Auth0 using email
async function getUserFromEmail(email) {
  const token = process.env.AUTH0_MANAGEMENT_API_TOKEN;  // Auth0 Management API Token from .env file
  const config = {
    method: 'get',
    url: `https://${process.env.AUTH0_DOMAIN}/api/v2/users-by-email?email=${encodeURIComponent(email)}`,
    headers: {
      'Authorization': `Bearer ${token}`,
      'Accept': 'application/json'
    }
  };

  try {
    const response = await axios.request(config);
    console.log('Fetched User Data:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error fetching user data:', error.response ? error.response.data : error.message);
    throw new Error('Failed to fetch user data from Auth0');
  }
}

// Route to fetch user data
app.get('/fetch-user/:email', async (req, res) => {
  try {
    const email = req.params.email;
    const userData = await getUserFromEmail(email);
    res.json(userData);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});

Error Message:

Error fetching user data: Cannot read properties of undefined (reading ‘request’) Failed to fetch user data from Auth0

What I’ve Tried:

  1. Verified that the Auth0 Management API token (AUTH0_MANAGEMENT_API_TOKEN) and domain (AUTH0_DOMAIN) are correctly set in my .env file.

  2. Checked that the API endpoint URL for Auth0 (/api/v2/users-by-email) is correct and accessible.

  3. Logged the token and configuration to ensure they are being set correctly before making the Axios request.

  4. Tried updating the Axios request syntax, but the error persists.

Environment:

  • Node.js version: 14.x

  • Express version: 4.x

  • Axios version: 0.21.x

Question:

How can I resolve the "Cannot read properties of undefined (reading 'request')" error, and successfully retrieve user data from Auth0? Is this an issue with how I’m making the Axios request, or could it be a configuration problem on the Auth0 side? Any insights or guidance would be greatly appreciated!