netlifyfaunadb

FaunaDB returns empty array (FaunaDB + Netlify + VueJS)


My code is based on the repository - https://github.com/ttntm/recept0r-ts

Code from "\functions\read-all.js":

const faunadb = require('faunadb');
const fnHeaders = require('./_shared/headers.js');

exports.handler = (event, context) => {

  const client = new faunadb.Client({
    secret: process.env.FAUNA_SECRET,
    domain: 'db.fauna.com',
    scheme: 'https',
    port: '443'
  });

  const q = faunadb.query;
  const headers = { ...fnHeaders };
  const origin = event.headers.Origin || event.headers.origin;
  headers['Access-Control-Allow-Origin'] = origin ? origin : '*';

  return client.query(q.Paginate(q.Match(q.Index('all_users'), false), { size: 500 }))
    .then((response) => {
      const listRefs = response.data;
      const getListDataQuery = listRefs.map(ref => q.Get(ref));  // create new query out of list refs, then query the refs
      return client.query(getListDataQuery).then((records) => {
        return { statusCode: 200, headers: headers, body: JSON.stringify(records) }
      })
    })
    .catch((error) => {
      return { statusCode: 400, headers: headers, body: JSON.stringify(error) }
    });
}

Code from "\src\store\modules\data.js":

async readAll({ commit, dispatch, rootGetters })
{
  const fn       = rootGetters['app/functions'];
  const request  = await fetch(fn.readAll, { method: 'GET' });
  const response = await request.json();

  if (response.length > 0) {
    commit('SET_ALL_RECIPES', response);
    commit('SET_LAST_UPDATED', new Date); }
  else {
    dispatch('app/sendToastMessage', { text: 'Error loading recipes. Please try again later.', type: 'error' }, { root: true });
    return 'error';
  }
}

Everything seems to be set. For example, this code works:

client.query(q.CreateCollection({ name: 'someCollection' }))

But can't read any data.

If launch application by "netlify dev" (localhost) - "read-all" returns empty array ("[]").

If launch application by "network" - "read-all" returns default "index.html".

I have no idea what's wrong. Maybe someone give advice...


I found a similar question - Local Netlify function server gives strange response instead of FaunaDB data

Some answer:

"In my experience, one of the most common reasons for this error is a routing problem, which is triggering a 404 response route serving HTML instead of your expected function handler."


Solution

  • This code works:

      return client.query(q.Paginate(q.Documents(q.Collection('customers')), { size: 500 }))
        .then((response) => {
          const listRefs = response.data;
          const getListDataQuery = listRefs.map(ref => q.Get(ref));  // create new query out of list refs, then query the refs      
          return client.query(getListDataQuery).then((records) => {
            return { statusCode: 200, headers: headers, body: JSON.stringify(records) }
          });
        })
        .catch((error) => {
          return { statusCode: 400, headers: headers, body: JSON.stringify(error) }
        });