adrianhajdin / project_ai_mern_image_generation

Build and Deploy a Full Stack MERN AI Image Generation App MidJourney & DALL E Clone
https://jsmastery.pro
1.09k stars 303 forks source link

SyntaxError: Unexpected token 'O', "OK" is not valid JSON while loading posts from mongodb in home.jsx #19

Closed krishnadev7 closed 1 year ago

krishnadev7 commented 1 year ago

getting following error while fetching posts from the mongodb Screenshot (2) it is showing error when i do response data to json format

at this part

 if (response.status == 200) {
        const result = await response.json();
        setAllPosts(result.data.reverse());
      }

successfully fetching posts from the mongodb when i doconsole.log(response) Screenshot (3)

krishnadev7 commented 1 year ago
// Get All Post
router.route('/').get(async (req, res) => {
  try {
    const posts = await post.find({});
    console.log(posts);
    res.json({ success: true, data: posts }); // changed to res.json instead of res.status
  } catch (error) {
    res.sendStatus(500).json({
      success: false,
      message: 'Fetching posts failed, please try again',
    });
  }
});