Angular : How to get constant values in the component after setting them?










0














I've a constant array of orders like this in constants.ts file



 export const ORDERS = [

'id': 'PROCESSING',
'displayName': null
,

'id': 'SHIPPED',
'displayName': null

];


Here in my component I'm setting the value of 'displayName'



import ORDERS from './constants';
-----------------
--------------
setOrders()
ORDERS.forEach((order: any) =>

if(order.id === 'PROCESSING')
order.displayName = 'Processing';


if(order.id === 'SHIPPED')
order.displayName = 'Shipped';


);



But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'



getOrders() 
ORDERS.forEach((order: any) =>
console.log(order); // displayName exists in the object
console.log(order.displayName); // But here null
);



Both functions are in a same component. Please help me how to resolve this thanks.



Update : Full implementation



getTimePeriod() 
return Promise.resolve((() =>

this.orderService.getTimePeriod()
.subscribe(
(data) =>
this.orderStatusList = data.configParameters;
this.orderStatusList.forEach(
(s: any) =>
if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);

if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);

);
,
error =>
this.errorMessage = this.orderService.handleError(error);

);
)());


setOrders(status, name)
ORDERS.forEach((order: any) =>
if (order.id === status)
order.displayName = name;

);


getOrders()
return Promise.resolve((() =>

ORDERS.forEach((order: any) =>
const displayName = order;

console.log(order); // displayName exists in order
console.log(displayName); // Null

this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
.subscribe(
(data) =>
this.ordersWidgetInfo.push(

...order,
...data,
number: data.orders && data.orders.length,
);
,
error =>
console.log(`error for $orderType : $error`);

);

);
)());










share|improve this question




























    0














    I've a constant array of orders like this in constants.ts file



     export const ORDERS = [

    'id': 'PROCESSING',
    'displayName': null
    ,

    'id': 'SHIPPED',
    'displayName': null

    ];


    Here in my component I'm setting the value of 'displayName'



    import ORDERS from './constants';
    -----------------
    --------------
    setOrders()
    ORDERS.forEach((order: any) =>

    if(order.id === 'PROCESSING')
    order.displayName = 'Processing';


    if(order.id === 'SHIPPED')
    order.displayName = 'Shipped';


    );



    But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'



    getOrders() 
    ORDERS.forEach((order: any) =>
    console.log(order); // displayName exists in the object
    console.log(order.displayName); // But here null
    );



    Both functions are in a same component. Please help me how to resolve this thanks.



    Update : Full implementation



    getTimePeriod() 
    return Promise.resolve((() =>

    this.orderService.getTimePeriod()
    .subscribe(
    (data) =>
    this.orderStatusList = data.configParameters;
    this.orderStatusList.forEach(
    (s: any) =>
    if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);

    if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);

    );
    ,
    error =>
    this.errorMessage = this.orderService.handleError(error);

    );
    )());


    setOrders(status, name)
    ORDERS.forEach((order: any) =>
    if (order.id === status)
    order.displayName = name;

    );


    getOrders()
    return Promise.resolve((() =>

    ORDERS.forEach((order: any) =>
    const displayName = order;

    console.log(order); // displayName exists in order
    console.log(displayName); // Null

    this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
    .subscribe(
    (data) =>
    this.ordersWidgetInfo.push(

    ...order,
    ...data,
    number: data.orders && data.orders.length,
    );
    ,
    error =>
    console.log(`error for $orderType : $error`);

    );

    );
    )());










    share|improve this question


























      0












      0








      0







      I've a constant array of orders like this in constants.ts file



       export const ORDERS = [

      'id': 'PROCESSING',
      'displayName': null
      ,

      'id': 'SHIPPED',
      'displayName': null

      ];


      Here in my component I'm setting the value of 'displayName'



      import ORDERS from './constants';
      -----------------
      --------------
      setOrders()
      ORDERS.forEach((order: any) =>

      if(order.id === 'PROCESSING')
      order.displayName = 'Processing';


      if(order.id === 'SHIPPED')
      order.displayName = 'Shipped';


      );



      But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'



      getOrders() 
      ORDERS.forEach((order: any) =>
      console.log(order); // displayName exists in the object
      console.log(order.displayName); // But here null
      );



      Both functions are in a same component. Please help me how to resolve this thanks.



      Update : Full implementation



      getTimePeriod() 
      return Promise.resolve((() =>

      this.orderService.getTimePeriod()
      .subscribe(
      (data) =>
      this.orderStatusList = data.configParameters;
      this.orderStatusList.forEach(
      (s: any) =>
      if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);

      if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);

      );
      ,
      error =>
      this.errorMessage = this.orderService.handleError(error);

      );
      )());


      setOrders(status, name)
      ORDERS.forEach((order: any) =>
      if (order.id === status)
      order.displayName = name;

      );


      getOrders()
      return Promise.resolve((() =>

      ORDERS.forEach((order: any) =>
      const displayName = order;

      console.log(order); // displayName exists in order
      console.log(displayName); // Null

      this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
      .subscribe(
      (data) =>
      this.ordersWidgetInfo.push(

      ...order,
      ...data,
      number: data.orders && data.orders.length,
      );
      ,
      error =>
      console.log(`error for $orderType : $error`);

      );

      );
      )());










      share|improve this question















      I've a constant array of orders like this in constants.ts file



       export const ORDERS = [

      'id': 'PROCESSING',
      'displayName': null
      ,

      'id': 'SHIPPED',
      'displayName': null

      ];


      Here in my component I'm setting the value of 'displayName'



      import ORDERS from './constants';
      -----------------
      --------------
      setOrders()
      ORDERS.forEach((order: any) =>

      if(order.id === 'PROCESSING')
      order.displayName = 'Processing';


      if(order.id === 'SHIPPED')
      order.displayName = 'Shipped';


      );



      But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'



      getOrders() 
      ORDERS.forEach((order: any) =>
      console.log(order); // displayName exists in the object
      console.log(order.displayName); // But here null
      );



      Both functions are in a same component. Please help me how to resolve this thanks.



      Update : Full implementation



      getTimePeriod() 
      return Promise.resolve((() =>

      this.orderService.getTimePeriod()
      .subscribe(
      (data) =>
      this.orderStatusList = data.configParameters;
      this.orderStatusList.forEach(
      (s: any) =>
      if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);

      if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);

      );
      ,
      error =>
      this.errorMessage = this.orderService.handleError(error);

      );
      )());


      setOrders(status, name)
      ORDERS.forEach((order: any) =>
      if (order.id === status)
      order.displayName = name;

      );


      getOrders()
      return Promise.resolve((() =>

      ORDERS.forEach((order: any) =>
      const displayName = order;

      console.log(order); // displayName exists in order
      console.log(displayName); // Null

      this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
      .subscribe(
      (data) =>
      this.ordersWidgetInfo.push(

      ...order,
      ...data,
      number: data.orders && data.orders.length,
      );
      ,
      error =>
      console.log(`error for $orderType : $error`);

      );

      );
      )());







      angular typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:18

























      asked Nov 12 at 13:26









      Junaid

      164211




      164211






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.



          However, after trying your code, it seems to works just fine. See example on JSFiddle.



          This leads me to conclude that your problem is most likely that getOrders is being invoked before setOrders, thus the null values. To fix this, make sure you are calling setOrders before you call getOrders.






          share|improve this answer




















          • This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
            – Junaid
            Nov 12 at 13:48










          • Is there anywhere else in your code that these two methods are being called?
            – Lansana
            Nov 12 at 13:54










          • No just inside ngOnInit()
            – Junaid
            Nov 12 at 13:59










          • Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
            – Lansana
            Nov 12 at 14:02










          • I've updated my question
            – Junaid
            Nov 12 at 14:19










          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%2f53263184%2fangular-how-to-get-constant-values-in-the-component-after-setting-them%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.



          However, after trying your code, it seems to works just fine. See example on JSFiddle.



          This leads me to conclude that your problem is most likely that getOrders is being invoked before setOrders, thus the null values. To fix this, make sure you are calling setOrders before you call getOrders.






          share|improve this answer




















          • This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
            – Junaid
            Nov 12 at 13:48










          • Is there anywhere else in your code that these two methods are being called?
            – Lansana
            Nov 12 at 13:54










          • No just inside ngOnInit()
            – Junaid
            Nov 12 at 13:59










          • Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
            – Lansana
            Nov 12 at 14:02










          • I've updated my question
            – Junaid
            Nov 12 at 14:19















          0














          The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.



          However, after trying your code, it seems to works just fine. See example on JSFiddle.



          This leads me to conclude that your problem is most likely that getOrders is being invoked before setOrders, thus the null values. To fix this, make sure you are calling setOrders before you call getOrders.






          share|improve this answer




















          • This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
            – Junaid
            Nov 12 at 13:48










          • Is there anywhere else in your code that these two methods are being called?
            – Lansana
            Nov 12 at 13:54










          • No just inside ngOnInit()
            – Junaid
            Nov 12 at 13:59










          • Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
            – Lansana
            Nov 12 at 14:02










          • I've updated my question
            – Junaid
            Nov 12 at 14:19













          0












          0








          0






          The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.



          However, after trying your code, it seems to works just fine. See example on JSFiddle.



          This leads me to conclude that your problem is most likely that getOrders is being invoked before setOrders, thus the null values. To fix this, make sure you are calling setOrders before you call getOrders.






          share|improve this answer












          The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.



          However, after trying your code, it seems to works just fine. See example on JSFiddle.



          This leads me to conclude that your problem is most likely that getOrders is being invoked before setOrders, thus the null values. To fix this, make sure you are calling setOrders before you call getOrders.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 13:34









          Lansana

          4,68022050




          4,68022050











          • This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
            – Junaid
            Nov 12 at 13:48










          • Is there anywhere else in your code that these two methods are being called?
            – Lansana
            Nov 12 at 13:54










          • No just inside ngOnInit()
            – Junaid
            Nov 12 at 13:59










          • Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
            – Lansana
            Nov 12 at 14:02










          • I've updated my question
            – Junaid
            Nov 12 at 14:19
















          • This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
            – Junaid
            Nov 12 at 13:48










          • Is there anywhere else in your code that these two methods are being called?
            – Lansana
            Nov 12 at 13:54










          • No just inside ngOnInit()
            – Junaid
            Nov 12 at 13:59










          • Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
            – Lansana
            Nov 12 at 14:02










          • I've updated my question
            – Junaid
            Nov 12 at 14:19















          This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
          – Junaid
          Nov 12 at 13:48




          This is bizzare. Although I'm returning promise and executing methods like this this.setOrders().then(() => this.getOrders());
          – Junaid
          Nov 12 at 13:48












          Is there anywhere else in your code that these two methods are being called?
          – Lansana
          Nov 12 at 13:54




          Is there anywhere else in your code that these two methods are being called?
          – Lansana
          Nov 12 at 13:54












          No just inside ngOnInit()
          – Junaid
          Nov 12 at 13:59




          No just inside ngOnInit()
          – Junaid
          Nov 12 at 13:59












          Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
          – Lansana
          Nov 12 at 14:02




          Show your entire implementation of setOrders. I assume the issue is inside there given all the information so far.
          – Lansana
          Nov 12 at 14:02












          I've updated my question
          – Junaid
          Nov 12 at 14:19




          I've updated my question
          – Junaid
          Nov 12 at 14:19

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53263184%2fangular-how-to-get-constant-values-in-the-component-after-setting-them%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線