使用Next
API 路由和Knex
+ MySQL
,并使用React
andSWR
进行获取,我遇到了一个奇怪的错误。如果请求失败,我的查询会开始附加, *
到select
语句中,从而导致 SQL 语法错误。例如,查询应该使用select *
,但输出为select *, *
and thenselect *, *, *
等等。有谁知道为什么会发生这种情况?
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 });