Having trouble fetching data from my database in a production setting. The code functions fine on localhost, but fails to grab updated data when live. Below is the relevant snippet:
import {connect} from "@/dbConnection/dbConnection";
import Post from "@/modals/postModal";
export const GET = async () => {
console.log('Request Made on Get Posts...' );
try{
await connect();
const posts = await Post.find().sort({createdAt: -1}).limit(20);
if(!posts){
return Response.json({error: "Posts not found"});
}
return Response.json({posts});
}catch (e) {
return Response.json({error: "Database error"});
}
}
Data retrieval code :
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
setLoading(true)
setTimeout(
() => {
fetchPosts().then((r) => {
setPosts(r.posts)
setLoading(false)
}).catch((e) => {
setError("Error fetching posts")
setLoading(false)
});
}
, 2000
)
}
, []);
async function fetchPosts() {
try {
const response = await axios.get('/api/post/getblogs');
return response.data;
} catch (error) {
setError("Error fetching posts")
}
}
Production link :
Visit here
Steps taken:
Confirmed correct database connection setup. Verified functionality on localhost with success. Checked logs for any issues, but no errors were found.
Expected outcome:
Upon requesting post data, the code should retrieve the most recent information from the database.