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表。我会试试的…谢谢你的帮助!我在写了这个问题之后才意识到这一点。有时候我的大脑太模糊了,以至于在我写出问题之前无法思考。