Fetch By Id Error
Unanswered
Singapura posted this in #help-forum
SingapuraOP
Please ping me what I'm doing wrong
20 Replies
@Singapura Please ping me what I'm doing wrong
how did you define getBlogById (or import it)
SingapuraOP
import BlogDetails from '@/components/blogs/blogdetails';
import { getBlogById } from '@/duck/network';
export default async function page({ params }) {
const data = await getBlogById(params?.id);
return (
data.length > 0 && <BlogDetails blog={data} />
);
}
import { getBlogById } from '@/duck/network';
export default async function page({ params }) {
const data = await getBlogById(params?.id);
return (
data.length > 0 && <BlogDetails blog={data} />
);
}
@riský
i think the error is saying it somehow failing to resolve and use your function
i dont know why tho, as i assume you have the folders and file correctly
just make sure to delete
.next folder (clear cache incase it died) and maybe update nextjsSingapuraOP
The folder and file are placed correctly still it is an issue
@riský just make sure to delete `.next` folder (clear cache incase it died) and maybe update nextjs
SingapuraOP
ALready did it
ok, fair
what does your dock/network file look like
(only really need to see how getBlogById is exported)
SingapuraOP
// src/duck/network.js
import axiosInstance from '@/path/to/axiosInstance';
import { toast } from 'react-toastify';
export const getBlogById = async (id) => {
try {
const response = await axiosInstance.get(
return response.data;
} catch (error) {
console.error(error, 'error');
toast.error('Error fetching Details of blogs');
return error.response.data.data;
}
};
import axiosInstance from '@/path/to/axiosInstance';
import { toast } from 'react-toastify';
export const getBlogById = async (id) => {
try {
const response = await axiosInstance.get(
/api/${id});return response.data;
} catch (error) {
console.error(error, 'error');
toast.error('Error fetching Details of blogs');
return error.response.data.data;
}
};
AHH this looks all legit
somehow the issue is the bundled file is making the function not a function, so when you use it in page its broken
SingapuraOP
Yes
and you are sure the
@/ import works here (i see you not using typescript so just checking if you have another page working with it) - also if you relative ../../xyz.js still sad?SingapuraOP
there is not a path issue
@Singapura // src/duck/network.js
import axiosInstance from '@/path/to/axiosInstance';
import { toast } from 'react-toastify';
export const getBlogById = async (id) => {
try {
const response = await axiosInstance.get(`/api/${id}`);
return response.data;
} catch (error) {
console.error(error, 'error');
toast.error('Error fetching Details of blogs');
return error.response.data.data;
}
};
do you have "use client" in this file by any chances? btw toast doesn't work in the server so won't work here
SingapuraOP
@joulev I have removed the toast also but it still doesn't work
Could you make a minimal reproduction repository? https://l.joulev.dev/mrr