I have React with MobX function not working in the order I expect. Any Ideas?












0















I am working on a React project the I am using Mobx for stores. I am getting the store to work right I am saving data without any problems. The one problem I am running into is I call a couple of functions and it seems to be they are being run backwards and I can not figure it out. I will list the code below, if someone could look at it with fresh eyes and tell me what I am missing.



Main .js file



componentDidMount(){
const query = parse(location.search);
if (query.slug !== undefined) {
this.props.IftaStore.getTruck(query.slug);
this.props.IftaStore.load();
} else {
this.props.IftaStore.setLimitByTruck(false);
this.props.IftaStore.load();
}
}


I basically have a url get that I am getting called slug. I parse that then if it is not undefined I call a function called getTruck in my store (see below). Once getTruck is run then the function load should be called (also in the store). What seems to be happening is load is running then getTruck.



store code:



getTruck = async user => {
const thisConst = this;
let endpoint = '/api/equipment/truck/?driver=' + user;
let lookupOptions = {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
};
let data = ;
try {
const response = await fetch(endpoint, lookupOptions);
const data = await response.json();
thisConst.trucknumber = data.results[0]["trucknumber"];
thisConst.limit_by_truck = true;
} catch (e) {
console.log(e);
}
console.log("getTruck trucknumber: ", thisConst.trucknumber);


if (data.count === 0) {
alert("You are not assigned to a truck, please contact your Driver Manager")
}
};

load = async nextEndpoint => {
const thisConst = this;
let data = ;
let endpoint = '/api/dispatch/ifta_stand/';
let lookupOptions = {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
};
console.log("load truck number: ", thisConst.trucknumber);

if (thisConst.limit_by_truck) {
endpoint = '/api/dispatch/ifta_stand/?truck_number=' + thisConst.trucknumber;
} else {
endpoint = '/api/dispatch/ifta_stand/'
}
if (nextEndpoint !== undefined) {
endpoint = nextEndpoint;
}

try {
const response = await fetch(endpoint, lookupOptions);
const data = await response.json();
thisConst.ifta_stand = data.results;
thisConst.next = data.next;
thisConst.previous = data.previous;
thisConst.count = data.count;
} catch(e) {
console.log(e);
}

};


when I run this app the console logs look like this:



iftaStore.js:138 load truck number:  0
iftaStore.js:100 getTruck trucknumber: 24


it should be reversed the getTruck trucknumber should log before the load truck number.



could someone please look at my code and let me know what I am missing...



Thank you all very much for your time.










share|improve this question



























    0















    I am working on a React project the I am using Mobx for stores. I am getting the store to work right I am saving data without any problems. The one problem I am running into is I call a couple of functions and it seems to be they are being run backwards and I can not figure it out. I will list the code below, if someone could look at it with fresh eyes and tell me what I am missing.



    Main .js file



    componentDidMount(){
    const query = parse(location.search);
    if (query.slug !== undefined) {
    this.props.IftaStore.getTruck(query.slug);
    this.props.IftaStore.load();
    } else {
    this.props.IftaStore.setLimitByTruck(false);
    this.props.IftaStore.load();
    }
    }


    I basically have a url get that I am getting called slug. I parse that then if it is not undefined I call a function called getTruck in my store (see below). Once getTruck is run then the function load should be called (also in the store). What seems to be happening is load is running then getTruck.



    store code:



    getTruck = async user => {
    const thisConst = this;
    let endpoint = '/api/equipment/truck/?driver=' + user;
    let lookupOptions = {
    method: "GET",
    headers: {
    'Content-Type': 'application/json'
    }
    };
    let data = ;
    try {
    const response = await fetch(endpoint, lookupOptions);
    const data = await response.json();
    thisConst.trucknumber = data.results[0]["trucknumber"];
    thisConst.limit_by_truck = true;
    } catch (e) {
    console.log(e);
    }
    console.log("getTruck trucknumber: ", thisConst.trucknumber);


    if (data.count === 0) {
    alert("You are not assigned to a truck, please contact your Driver Manager")
    }
    };

    load = async nextEndpoint => {
    const thisConst = this;
    let data = ;
    let endpoint = '/api/dispatch/ifta_stand/';
    let lookupOptions = {
    method: "GET",
    headers: {
    'Content-Type': 'application/json'
    }
    };
    console.log("load truck number: ", thisConst.trucknumber);

    if (thisConst.limit_by_truck) {
    endpoint = '/api/dispatch/ifta_stand/?truck_number=' + thisConst.trucknumber;
    } else {
    endpoint = '/api/dispatch/ifta_stand/'
    }
    if (nextEndpoint !== undefined) {
    endpoint = nextEndpoint;
    }

    try {
    const response = await fetch(endpoint, lookupOptions);
    const data = await response.json();
    thisConst.ifta_stand = data.results;
    thisConst.next = data.next;
    thisConst.previous = data.previous;
    thisConst.count = data.count;
    } catch(e) {
    console.log(e);
    }

    };


    when I run this app the console logs look like this:



    iftaStore.js:138 load truck number:  0
    iftaStore.js:100 getTruck trucknumber: 24


    it should be reversed the getTruck trucknumber should log before the load truck number.



    could someone please look at my code and let me know what I am missing...



    Thank you all very much for your time.










    share|improve this question

























      0












      0








      0








      I am working on a React project the I am using Mobx for stores. I am getting the store to work right I am saving data without any problems. The one problem I am running into is I call a couple of functions and it seems to be they are being run backwards and I can not figure it out. I will list the code below, if someone could look at it with fresh eyes and tell me what I am missing.



      Main .js file



      componentDidMount(){
      const query = parse(location.search);
      if (query.slug !== undefined) {
      this.props.IftaStore.getTruck(query.slug);
      this.props.IftaStore.load();
      } else {
      this.props.IftaStore.setLimitByTruck(false);
      this.props.IftaStore.load();
      }
      }


      I basically have a url get that I am getting called slug. I parse that then if it is not undefined I call a function called getTruck in my store (see below). Once getTruck is run then the function load should be called (also in the store). What seems to be happening is load is running then getTruck.



      store code:



      getTruck = async user => {
      const thisConst = this;
      let endpoint = '/api/equipment/truck/?driver=' + user;
      let lookupOptions = {
      method: "GET",
      headers: {
      'Content-Type': 'application/json'
      }
      };
      let data = ;
      try {
      const response = await fetch(endpoint, lookupOptions);
      const data = await response.json();
      thisConst.trucknumber = data.results[0]["trucknumber"];
      thisConst.limit_by_truck = true;
      } catch (e) {
      console.log(e);
      }
      console.log("getTruck trucknumber: ", thisConst.trucknumber);


      if (data.count === 0) {
      alert("You are not assigned to a truck, please contact your Driver Manager")
      }
      };

      load = async nextEndpoint => {
      const thisConst = this;
      let data = ;
      let endpoint = '/api/dispatch/ifta_stand/';
      let lookupOptions = {
      method: "GET",
      headers: {
      'Content-Type': 'application/json'
      }
      };
      console.log("load truck number: ", thisConst.trucknumber);

      if (thisConst.limit_by_truck) {
      endpoint = '/api/dispatch/ifta_stand/?truck_number=' + thisConst.trucknumber;
      } else {
      endpoint = '/api/dispatch/ifta_stand/'
      }
      if (nextEndpoint !== undefined) {
      endpoint = nextEndpoint;
      }

      try {
      const response = await fetch(endpoint, lookupOptions);
      const data = await response.json();
      thisConst.ifta_stand = data.results;
      thisConst.next = data.next;
      thisConst.previous = data.previous;
      thisConst.count = data.count;
      } catch(e) {
      console.log(e);
      }

      };


      when I run this app the console logs look like this:



      iftaStore.js:138 load truck number:  0
      iftaStore.js:100 getTruck trucknumber: 24


      it should be reversed the getTruck trucknumber should log before the load truck number.



      could someone please look at my code and let me know what I am missing...



      Thank you all very much for your time.










      share|improve this question














      I am working on a React project the I am using Mobx for stores. I am getting the store to work right I am saving data without any problems. The one problem I am running into is I call a couple of functions and it seems to be they are being run backwards and I can not figure it out. I will list the code below, if someone could look at it with fresh eyes and tell me what I am missing.



      Main .js file



      componentDidMount(){
      const query = parse(location.search);
      if (query.slug !== undefined) {
      this.props.IftaStore.getTruck(query.slug);
      this.props.IftaStore.load();
      } else {
      this.props.IftaStore.setLimitByTruck(false);
      this.props.IftaStore.load();
      }
      }


      I basically have a url get that I am getting called slug. I parse that then if it is not undefined I call a function called getTruck in my store (see below). Once getTruck is run then the function load should be called (also in the store). What seems to be happening is load is running then getTruck.



      store code:



      getTruck = async user => {
      const thisConst = this;
      let endpoint = '/api/equipment/truck/?driver=' + user;
      let lookupOptions = {
      method: "GET",
      headers: {
      'Content-Type': 'application/json'
      }
      };
      let data = ;
      try {
      const response = await fetch(endpoint, lookupOptions);
      const data = await response.json();
      thisConst.trucknumber = data.results[0]["trucknumber"];
      thisConst.limit_by_truck = true;
      } catch (e) {
      console.log(e);
      }
      console.log("getTruck trucknumber: ", thisConst.trucknumber);


      if (data.count === 0) {
      alert("You are not assigned to a truck, please contact your Driver Manager")
      }
      };

      load = async nextEndpoint => {
      const thisConst = this;
      let data = ;
      let endpoint = '/api/dispatch/ifta_stand/';
      let lookupOptions = {
      method: "GET",
      headers: {
      'Content-Type': 'application/json'
      }
      };
      console.log("load truck number: ", thisConst.trucknumber);

      if (thisConst.limit_by_truck) {
      endpoint = '/api/dispatch/ifta_stand/?truck_number=' + thisConst.trucknumber;
      } else {
      endpoint = '/api/dispatch/ifta_stand/'
      }
      if (nextEndpoint !== undefined) {
      endpoint = nextEndpoint;
      }

      try {
      const response = await fetch(endpoint, lookupOptions);
      const data = await response.json();
      thisConst.ifta_stand = data.results;
      thisConst.next = data.next;
      thisConst.previous = data.previous;
      thisConst.count = data.count;
      } catch(e) {
      console.log(e);
      }

      };


      when I run this app the console logs look like this:



      iftaStore.js:138 load truck number:  0
      iftaStore.js:100 getTruck trucknumber: 24


      it should be reversed the getTruck trucknumber should log before the load truck number.



      could someone please look at my code and let me know what I am missing...



      Thank you all very much for your time.







      django reactjs mobx mobx-react






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 2:32









      Rich SchmittRich Schmitt

      34




      34
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439994%2fi-have-react-with-mobx-function-not-working-in-the-order-i-expect-any-ideas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439994%2fi-have-react-with-mobx-function-not-working-in-the-order-i-expect-any-ideas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'