When making API requests using Next API routes and interacting with Knex + MySQL, along with utilizing React and SWR for data fetching, I encountered a strange issue. If a request fails, my SQL queries start to append ", *" to the "select" statement, causing SQL syntax errors. For instance, what should be a query like "select *", ends up as "select *, *" and then progresses to "select *, *, *" and so forth. Does anyone have any insights into why this could be happening?
Fetching data with 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 query:
const User = knex(TABLE_NAMES.user);
export const readById = (id) => User.select('*').where({ id });