使用Next
应用编程接口路由和Knex
+ MySQL
,并使用React
和SWR
获取,我得到了一个奇怪的bug。如果请求失败,我的查询将开始将, *
附加到select
语句,从而导致SQL语法错误。例如,查询应该使用select *
,但结果是select *, *
,然后是select *, *, *
,依此类推。有人知道为什么会发生这种情况吗?
SWR获取:
export const swrFetcher = async (...args) => {
const [url, contentType = 'application/json'] = args;
const res = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': contentType,
},
});
if (!res.ok) throw new Error(res.statusText);
const json = await res.json();
return json;
};
const { data, error } = useSWR('/api/user/me', swrFetcher, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateOnMount: true,
});
knex查询:
const User = knex(TABLE_NAMES.user);
export const readById = (id) => User.select('*').where({ id });
发布于 2021-01-20 01:50:04
您可能需要在函数调用中创建knex
实例,而不是像现在这样每次都重用相同的实例。
export const readById = (id) => {
const User = knex(TABLE_NAMES.user);
return User.select('*').where({ id });
}
https://stackoverflow.com/questions/65796511
复制相似问题