Can't call object properly with promises









up vote
0
down vote

favorite












My code was working fine until I decided I wanted to try and return two values in my promise chain to the next function. I can call one of the values but not the other.



My code looks like this



app.get('/projects', (req, res) => 
practice.screamIt('matt').then((name) =>
return [practice.translateIt(name), name]; //puts name as the parameter for next function
).then((translate) =>
console.log(translate[1] + ' test occured here')
console.log(translate[0][0].englishName)
return [`The name you entered is $translate[1]`, `$translate[0].englishName is $translate[0].spanishName in spanish`]
).then((value)=>

res.render('projects',
pageTitle: "Projects Page",
practice: practice,
value1: value[0],
value2: value[1]
);

).catch((errorMessage)=>
console.log(errorMessage)
)

);


And when I log the first bit of data it shows:



[ Promise englishName: 'Matt', spanishName: 'Mateo' ,'Matt' ]


I want to be able to call englishName, but can't seem to do so without getting undefined. I need to be able to call englishName in order for my second function work as intended.










share|improve this question



















  • 1




    is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
    – WakeskaterX
    Nov 9 at 14:09















up vote
0
down vote

favorite












My code was working fine until I decided I wanted to try and return two values in my promise chain to the next function. I can call one of the values but not the other.



My code looks like this



app.get('/projects', (req, res) => 
practice.screamIt('matt').then((name) =>
return [practice.translateIt(name), name]; //puts name as the parameter for next function
).then((translate) =>
console.log(translate[1] + ' test occured here')
console.log(translate[0][0].englishName)
return [`The name you entered is $translate[1]`, `$translate[0].englishName is $translate[0].spanishName in spanish`]
).then((value)=>

res.render('projects',
pageTitle: "Projects Page",
practice: practice,
value1: value[0],
value2: value[1]
);

).catch((errorMessage)=>
console.log(errorMessage)
)

);


And when I log the first bit of data it shows:



[ Promise englishName: 'Matt', spanishName: 'Mateo' ,'Matt' ]


I want to be able to call englishName, but can't seem to do so without getting undefined. I need to be able to call englishName in order for my second function work as intended.










share|improve this question



















  • 1




    is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
    – WakeskaterX
    Nov 9 at 14:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My code was working fine until I decided I wanted to try and return two values in my promise chain to the next function. I can call one of the values but not the other.



My code looks like this



app.get('/projects', (req, res) => 
practice.screamIt('matt').then((name) =>
return [practice.translateIt(name), name]; //puts name as the parameter for next function
).then((translate) =>
console.log(translate[1] + ' test occured here')
console.log(translate[0][0].englishName)
return [`The name you entered is $translate[1]`, `$translate[0].englishName is $translate[0].spanishName in spanish`]
).then((value)=>

res.render('projects',
pageTitle: "Projects Page",
practice: practice,
value1: value[0],
value2: value[1]
);

).catch((errorMessage)=>
console.log(errorMessage)
)

);


And when I log the first bit of data it shows:



[ Promise englishName: 'Matt', spanishName: 'Mateo' ,'Matt' ]


I want to be able to call englishName, but can't seem to do so without getting undefined. I need to be able to call englishName in order for my second function work as intended.










share|improve this question















My code was working fine until I decided I wanted to try and return two values in my promise chain to the next function. I can call one of the values but not the other.



My code looks like this



app.get('/projects', (req, res) => 
practice.screamIt('matt').then((name) =>
return [practice.translateIt(name), name]; //puts name as the parameter for next function
).then((translate) =>
console.log(translate[1] + ' test occured here')
console.log(translate[0][0].englishName)
return [`The name you entered is $translate[1]`, `$translate[0].englishName is $translate[0].spanishName in spanish`]
).then((value)=>

res.render('projects',
pageTitle: "Projects Page",
practice: practice,
value1: value[0],
value2: value[1]
);

).catch((errorMessage)=>
console.log(errorMessage)
)

);


And when I log the first bit of data it shows:



[ Promise englishName: 'Matt', spanishName: 'Mateo' ,'Matt' ]


I want to be able to call englishName, but can't seem to do so without getting undefined. I need to be able to call englishName in order for my second function work as intended.







javascript node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 14:18









Sridhar

4,90732636




4,90732636










asked Nov 9 at 14:04









noobcoderiam

347




347







  • 1




    is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
    – WakeskaterX
    Nov 9 at 14:09













  • 1




    is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
    – WakeskaterX
    Nov 9 at 14:09








1




1




is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
– WakeskaterX
Nov 9 at 14:09





is practice.translateIt(name) a promise? It looks like you're returning a promise in an array which won't work properly with the then I believe. I may be mistaken about that but I'm pretty sure that won't work.
– WakeskaterX
Nov 9 at 14:09













3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










You need to first resolve the translateIt() promise and use another then() to create the array



Change:



return [practice.translateIt(name), name];


To



return practice.translateIt(name).then(translate => [translate, name]);





share|improve this answer



























    up vote
    0
    down vote













    I don't have a full copy if your code but it appears you're returning a Promise in an Array to the then handler which I don't believe will work?






    practice.screamIt('matt').then((name) => 
    return practice.translateIt(name);
    ).then((translate) =>
    ...etc
    ).catch((errorMessage)=>
    console.log(errorMessage)
    );





    If you try something like that I believe that would work for you as it would be returning the result once the promise resolves.






    share|improve this answer




















    • Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
      – noobcoderiam
      Nov 9 at 14:13











    • Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
      – WakeskaterX
      Nov 9 at 14:36











    • I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
      – noobcoderiam
      Nov 9 at 15:12

















    up vote
    0
    down vote













    I suppose you need to return Promise.all([practice.translateIt(name), name]) instead of practice.translateIt(name) as I its returning a promise.



    That should return a single promise resolving to an array of values.






    share|improve this answer






















      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',
      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%2f53227215%2fcant-call-object-properly-with-promises%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You need to first resolve the translateIt() promise and use another then() to create the array



      Change:



      return [practice.translateIt(name), name];


      To



      return practice.translateIt(name).then(translate => [translate, name]);





      share|improve this answer
























        up vote
        2
        down vote



        accepted










        You need to first resolve the translateIt() promise and use another then() to create the array



        Change:



        return [practice.translateIt(name), name];


        To



        return practice.translateIt(name).then(translate => [translate, name]);





        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You need to first resolve the translateIt() promise and use another then() to create the array



          Change:



          return [practice.translateIt(name), name];


          To



          return practice.translateIt(name).then(translate => [translate, name]);





          share|improve this answer












          You need to first resolve the translateIt() promise and use another then() to create the array



          Change:



          return [practice.translateIt(name), name];


          To



          return practice.translateIt(name).then(translate => [translate, name]);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 14:16









          charlietfl

          136k1285118




          136k1285118






















              up vote
              0
              down vote













              I don't have a full copy if your code but it appears you're returning a Promise in an Array to the then handler which I don't believe will work?






              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );





              If you try something like that I believe that would work for you as it would be returning the result once the promise resolves.






              share|improve this answer




















              • Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
                – noobcoderiam
                Nov 9 at 14:13











              • Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
                – WakeskaterX
                Nov 9 at 14:36











              • I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
                – noobcoderiam
                Nov 9 at 15:12














              up vote
              0
              down vote













              I don't have a full copy if your code but it appears you're returning a Promise in an Array to the then handler which I don't believe will work?






              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );





              If you try something like that I believe that would work for you as it would be returning the result once the promise resolves.






              share|improve this answer




















              • Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
                – noobcoderiam
                Nov 9 at 14:13











              • Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
                – WakeskaterX
                Nov 9 at 14:36











              • I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
                – noobcoderiam
                Nov 9 at 15:12












              up vote
              0
              down vote










              up vote
              0
              down vote









              I don't have a full copy if your code but it appears you're returning a Promise in an Array to the then handler which I don't believe will work?






              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );





              If you try something like that I believe that would work for you as it would be returning the result once the promise resolves.






              share|improve this answer












              I don't have a full copy if your code but it appears you're returning a Promise in an Array to the then handler which I don't believe will work?






              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );





              If you try something like that I believe that would work for you as it would be returning the result once the promise resolves.






              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );





              practice.screamIt('matt').then((name) => 
              return practice.translateIt(name);
              ).then((translate) =>
              ...etc
              ).catch((errorMessage)=>
              console.log(errorMessage)
              );






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 at 14:11









              WakeskaterX

              972515




              972515











              • Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
                – noobcoderiam
                Nov 9 at 14:13











              • Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
                – WakeskaterX
                Nov 9 at 14:36











              • I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
                – noobcoderiam
                Nov 9 at 15:12
















              • Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
                – noobcoderiam
                Nov 9 at 14:13











              • Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
                – WakeskaterX
                Nov 9 at 14:36











              • I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
                – noobcoderiam
                Nov 9 at 15:12















              Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
              – noobcoderiam
              Nov 9 at 14:13





              Yes it's what I was doing before. I was trying to return the name parameter with the promise as two separate values just to test some strings. I beleive i got it to work by using return practice.translateIt(name) && name; Let me run it and ill confirm
              – noobcoderiam
              Nov 9 at 14:13













              Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
              – WakeskaterX
              Nov 9 at 14:36





              Ah I see what you were going for - yeah the guy above has a better answer than mine. Haven't had my coffee yet :)
              – WakeskaterX
              Nov 9 at 14:36













              I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
              – noobcoderiam
              Nov 9 at 15:12




              I appreciate anyone who takes the time to help a brother out. Especially a noob like me. So thanks for helping me out!
              – noobcoderiam
              Nov 9 at 15:12










              up vote
              0
              down vote













              I suppose you need to return Promise.all([practice.translateIt(name), name]) instead of practice.translateIt(name) as I its returning a promise.



              That should return a single promise resolving to an array of values.






              share|improve this answer


























                up vote
                0
                down vote













                I suppose you need to return Promise.all([practice.translateIt(name), name]) instead of practice.translateIt(name) as I its returning a promise.



                That should return a single promise resolving to an array of values.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I suppose you need to return Promise.all([practice.translateIt(name), name]) instead of practice.translateIt(name) as I its returning a promise.



                  That should return a single promise resolving to an array of values.






                  share|improve this answer














                  I suppose you need to return Promise.all([practice.translateIt(name), name]) instead of practice.translateIt(name) as I its returning a promise.



                  That should return a single promise resolving to an array of values.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 14:19

























                  answered Nov 9 at 14:14









                  vibhor1997a

                  1,4101623




                  1,4101623



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227215%2fcant-call-object-properly-with-promises%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Darth Vader #20

                      Ondo