首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FaunaDB返回空数组(FaunaDB + Netlify + VueJS)

FaunaDB返回空数组(FaunaDB + Netlify + VueJS)
EN

Stack Overflow用户
提问于 2022-07-07 12:58:35
回答 1查看 36关注 0票数 0

我的代码基于存储库- https://github.com/ttntm/recept0r-ts

来自“\function\read-all.js”的代码:

代码语言:javascript
复制
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) }
    });
}

来自“\src\store\modules\data.js”的代码:

代码语言:javascript
复制
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';
  }
}

一切似乎都安排好了。例如,此代码工作如下:

代码语言:javascript
复制
client.query(q.CreateCollection({ name: 'someCollection' }))

但不能读取任何数据。

如果通过"netlify dev“(localhost) -”read“启动应用程序,则返回空数组("[]")。

如果通过“网络”-“所有读取”启动应用程序,则返回默认的"index.html“。

我不知道怎么回事。也许有人会给我建议..。

我发现了一个类似的问题- Local Netlify function server gives strange response instead of FaunaDB data

一些答案是:

“根据我的经验,这个错误最常见的原因之一是路由问题,它触发了一个为HTML404服务的响应路由,而不是您期望的函数处理程序。”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-09 05:23:08

此代码适用于:

代码语言:javascript
复制
  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) }
    });

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72898256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档