supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

Empty Data Array Returned when Fetching from Supabase using Node.js and node-fetch

Open DeekshithRajBasa opened this issue 1 year ago • 0 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I am encountering an issue when trying to fetch data from Supabase using Node.js and the node-fetch library. The response I receive is showing an empty data array (data: []) even though I expect data to be present in the 'contacts' table. It's worth noting that the request works as expected in Postman when hitting directly to https://${subdomain}.supabase.co/rest/v1/contacts when passing the auth token and API key, but encounters issues when making the same request in Node.js to the same url.

To Reproduce

  1. Replace the usage of the Supabase SDK with a direct request to the Supabase endpoint URL in a Node.js application. (https://${subdomain}.supabase.co/rest/v1/table-name)
  2. Make a GET request to the Supabase endpoint URL to fetch data.
  3. It works well in postman, but not through Node.js

Expected behavior

I expect to receive the data from the 'contacts' table when making a GET request to the Supabase endpoint, similar to the successful response in Postman.

Screenshots

Directly hitting https://${subdomain}.supabase.co/rest/v1/contacts from postman:

Screenshot 2024-01-03 at 12 27 56 PM

Trying to make call https://${subdomain}.supabase.co/rest/v1/contacts through Node.js

Here's the code

const { environment } = require('../../../db/supabase-client.js');
const fetch = require('node-fetch');

exports.getContacts = async (req, res) => {
    const access_token = req.headers.authorization;
    try {
        const headers = {
            'Content-Type': 'application/json',
            'Authorization': access_token,
            'Apikey': environment.supabaseKey,
            'Accept': '*/*',
        };
        const _res = await fetch('https://' + environment.subdomain + '.supabase.co/rest/v1/contacts', {
            method: 'GET',
            headers: headers,
        });

        if (_res.ok) {
            const _data = await _res.json();
            if (_data) {
                console.log(_data);
                res.status(200).send(_data);
            }
        } else {
            console.error('Failed to fetch data:', _res.statusText);
            res.status(_res.status).json({ message: _res.statusText });
        }

    } catch (error) {
        console.error('Unexpected error:', error);
        res.status(500).json({ message: '500', error });
    }
};

Screenshot 2024-01-03 at 12 26 11 PM

System information

  • OS: macOS 14.1.2
  • Browser: Chrome - Version 119.0.6045.199
  • Tool: Postman - v10.21
  • Version of supabase-js: 2.39.1
  • Version of Node.js: v18.17.0
  • Version of node-fetch : ^2.7.0

DeekshithRajBasa avatar Jan 03 '24 07:01 DeekshithRajBasa