Coder Social home page Coder Social logo

Extra call about mern-starter HOT 10 CLOSED

hashnode avatar hashnode commented on July 18, 2024
Extra call

from mern-starter.

Comments (10)

SOSANA avatar SOSANA commented on July 18, 2024

Can you share how you able to see this executing twice?

from mern-starter.

SoundBot avatar SoundBot commented on July 18, 2024

@SOSANA just add console.log inside getPosts function. Log message will appear twice.

from mern-starter.

SOSANA avatar SOSANA commented on July 18, 2024

@SoundBot good catch! check this out, seems to be on initial load we get it called twice where as if we route from post view back to main we only have expected results of executing once...

image

from mern-starter.

SoundBot avatar SoundBot commented on July 18, 2024

@SOSANA Yep. Any ideas how to fix this?

from mern-starter.

SOSANA avatar SOSANA commented on July 18, 2024

@SoundBot this is where the fun begins, fixing this :)

from mern-starter.

mayankchd avatar mayankchd commented on July 18, 2024

The Action fetchPosts is disptached twice that's why getPosts is called twice. I will look into that.

from mern-starter.

mayankchd avatar mayankchd commented on July 18, 2024

In server/util/fetchData.js do console.log(needs) we will get Actions.fetchPosts two times. So the problem is in

  const needs = components.reduce((prev, current) => {
    return (current.need || [])
      .concat((current.WrappedComponent ? current.WrappedComponent.need : []) || [])
      .concat(prev);
  }, []);

Will fix!

from mern-starter.

SOSANA avatar SOSANA commented on July 18, 2024

@SoundBot @mayankchd
/server/util/fetchData.js

export function fetchComponentData(dispatch, components, params) {
  const needs = components.reduce((prev, current) => {
    console.log('test');
    return (current.need || [])
      .concat((current.WrappedComponent ? current.WrappedComponent.need !== current.need : []) || [])
      .concat(prev);
  }, []);
  console.log(needs);
  const promises = needs.map(need => dispatch(need(params)));
  return Promise.all(promises);
}

/server/controllers/post.controllers.js

export function getPosts(req, res) {
  Post.find().sort('-dateAdded').exec((err, posts) => {
    if (err) {
      return res.status(500).send(err);
    }

    console.log('postTests');
    res.json({ posts });
  });
}

image

I noticed that there is only one function call from server, but fetch still shows logging twice

from mern-starter.

mayankchd avatar mayankchd commented on July 18, 2024

Fixed in #80

from mern-starter.

SOSANA avatar SOSANA commented on July 18, 2024

@SoundBot @mayankchd as per my last comment its called once from server from getPost in post.controller, but fetch shows still fetching twice, this for client?

from mern-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.