我的代码基于存储库- https://github.com/ttntm/recept0r-ts
来自“\function\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) }
});
}
来自“\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';
}
}
一切似乎都安排好了。例如,此代码工作如下:
client.query(q.CreateCollection({ name: 'someCollection' }))
但不能读取任何数据。
如果通过"netlify dev“(localhost) -”read“启动应用程序,则返回空数组("[]")。
如果通过“网络”-“所有读取”启动应用程序,则返回默认的"index.html“。
我不知道怎么回事。也许有人会给我建议..。
我发现了一个类似的问题- Local Netlify function server gives strange response instead of FaunaDB data
一些答案是:
“根据我的经验,这个错误最常见的原因之一是路由问题,它触发了一个为HTML404服务的响应路由,而不是您期望的函数处理程序。”
发布于 2022-07-09 05:23:08
此代码适用于:
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) }
});
https://stackoverflow.com/questions/72898256
复制相似问题