Next js、Knex 和 SWR 的奇怪查询错误

Strange query bug with Next js, Knex, and SWR

使用Next API路由和Knex + MySQL,并使用ReactSWR进行抓取,我得到一个奇怪的漏洞。如果请求失败,我的查询开始将 , * 附加到 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 });

您可能需要在函数调用中创建 knex 实例,而不是像当前正在发生的那样每次都重复使用相同的实例。

export const readById = (id) => {
    const User = knex(TABLE_NAMES.user);
    return User.select('*').where({ id });
}