I really don't understand why I can't get useSWR
to work in my app.
I have been trying for two days and can't seems to find the reason.
The normal fetch works fine calling the same address in the same function.
const address =server+ `/api/google/getData?term=` + endRow.name;
const fetcher = async (url) => await axios.get(url).then((res) => res.data);
const { data, error } = useSWR(address, fetcher);
//Always undefined
console.log(data)
//Gets the data
async function test() {
const res = await fetch(address)
console.log(await res.json())
}
test();
API method:
import { connectToDatabase } from '../../../util/mongodbUtil'
export default async (req, res) => {
const { db } = await connectToDatabase();
return new Promise(async (resolve, reject) => {
try{
res.status(201).json({ response: ["TESTDATA"], success: true })
resolve()
} catch (e) {
console.log(e)
res.status(400).json({ success: false })
resolve()
}
})
}