Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Next js、Knex和SWR的奇怪查询错误_Javascript_Next.js_Knex.js_Swr - Fatal编程技术网

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

Javascript Next js、Knex和SWR的奇怪查询错误,javascript,next.js,knex.js,swr,Javascript,Next.js,Knex.js,Swr,使用NextAPI路由和Knex+MySQL,使用React和SWR进行抓取,我得到了一个奇怪的bug。如果请求失败,我的查询开始将,*附加到select语句,从而导致SQL语法错误。例如,查询应该使用select*,但结果是select*,*,然后是select*,*,*等等。有人知道为什么会这样吗 SWR获取: export const swrFetcher = async (...args) => { const [url, contentType = 'application/

使用
Next
API路由和
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 });

您可能需要在函数调用中创建
knex
实例,而不是像现在这样每次都重复使用

export const readById=(id)=>{
const User=knex(表_NAMES.User);
返回User.select('*')。其中({id});
}

可能无法以这种方式重用knex表。我会试试的…谢谢你的帮助!我在写了这个问题之后才意识到这一点。有时候我的大脑太模糊了,以至于在我写出问题之前无法思考。