Search code examples
javascriptfirebasefirebase-realtime-databaseaxiosdiscord.js

Firebase Realtime Database returning undefined after Axios GET request


I am trying to receive data from a firebase realtime database for my discord bot. I am using the firebase REST API and the axios package for my requests.

The requests are returning 200 and okay but they are returning the data as 'undefined' and I am unsure why.

The database

This is the code I am running, with the auth code removed:

const axios = require('axios');

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('activitytest')
        .setDescription('Returns scriptedethqns activity.'),
    async execute(interaction, client) {
        await axios({
            method: 'get',
            url: 'https://timedbtester-default-rtdb.europe-west1.firebasedatabase.app/users/',
            params: {
                print: "pretty",
                auth: "0tPszWgsfm16PT3rFiXVqbGzmh7g4mSqCPRVE8ny"
            }
        })
        .catch(err => {
            console.log(err)
            interaction.reply("⛔ An error has occured, see console (add embed later)")
        })
        .then(response => {
            console.log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
            console.log(response.data)
        })

        interaction.reply("Test")
    },
};

I have tried requesting to the database with and without the /users/ section. This is firebase guide I have been following.


Solution

  • To access Firebase Realtime Database's REST API, the URL you access needs to end in .json, which isn't the case in your code here:

    url: 'https://timedbtester-default-rtdb.europe-west1.firebasedatabase.app/users/'
    

    This means that your code is actually accessing the Firebase console (which you can verify by pasting that URL into your browser), which explains why you're not getting the data back.

    To solve the problem, ensure the URL ends in .json.