Access property of object nested in array nested in object with a for loop MongooseTest for existence of nested JavaScript object keyFinding an Embedded Document by a specific property in Mongoose, Node.js, MongodDBAccessing nested JavaScript objects with string keyHow do I update/upsert a document in Mongoose?Expressjs Mongoose find nested embedded documents are undefinedmap function for objects (instead of arrays)How to loop through an array in mongodb?Undefined type of nested schema for mongooseTargeting a query in Mongo to find and email nested in an objectHow can I access an object from a Mongoose Schema through the form?

What is Cash Advance APR?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Is it necessary to use pronouns with the verb "essere"?

Did the UK lift the requirement for registering SIM cards?

How much theory knowledge is actually used while playing?

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

Change the color of a single dot in `ddot` symbol

How to convince somebody that he is fit for something else, but not this job?

C++ copy constructor called at return

What kind of floor tile is this?

How does electrical safety system work on ISS?

Can you use Vicious Mockery to win an argument or gain favours?

Why is the Sun approximated as a black body at ~ 5800 K?

Doesn't the system of the Supreme Court oppose justice?

How to make money from a browser who sees 5 seconds into the future of any web page?

How do you make your own symbol when Detexify fails?

Taxes on Dividends in a Roth IRA

awk assign to multiple variables at once

When were female captains banned from Starfleet?

What is the difference between lands and mana?

Is it allowed to activate the ability of multiple planeswalkers in a single turn?

Is there a nicer/politer/more positive alternative for "negates"?

Has any country ever had 2 former presidents in jail simultaneously?

Is this toilet slogan correct usage of the English language?



Access property of object nested in array nested in object with a for loop Mongoose


Test for existence of nested JavaScript object keyFinding an Embedded Document by a specific property in Mongoose, Node.js, MongodDBAccessing nested JavaScript objects with string keyHow do I update/upsert a document in Mongoose?Expressjs Mongoose find nested embedded documents are undefinedmap function for objects (instead of arrays)How to loop through an array in mongodb?Undefined type of nested schema for mongooseTargeting a query in Mongo to find and email nested in an objectHow can I access an object from a Mongoose Schema through the form?













0















I'm new to js (clearly). I'm using mongoose and I was wondering : why do I get an error while accessing property with a for loop?



I'm using a schema that contains embedded schema



Usr.findOne(numCli: req.body.numTar, function(err, doc)

if (doc)

var i = 0;
const lengthAcc = doc.acc.length;
//can access prop individually
console.log(doc.acc[lengthAcc-1].propOne);

// can't using for loop: TypeError: Cannot read property 'propOne' of undefined
for (i = 0; i < 10; i++)
console.log(doc.acc[lengthAcc - i].propOne);




);









share|improve this question






















  • Why to 10? AFAIK Mongoose does not do negative notation on lengths

    – Neil Lunn
    Mar 7 at 4:33












  • Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

    – ceg
    Mar 7 at 4:39











  • i=1, start i from 1

    – Şivā SankĂr
    Mar 7 at 4:41











  • I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

    – Neil Lunn
    Mar 7 at 4:53












  • Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

    – ceg
    Mar 7 at 4:55
















0















I'm new to js (clearly). I'm using mongoose and I was wondering : why do I get an error while accessing property with a for loop?



I'm using a schema that contains embedded schema



Usr.findOne(numCli: req.body.numTar, function(err, doc)

if (doc)

var i = 0;
const lengthAcc = doc.acc.length;
//can access prop individually
console.log(doc.acc[lengthAcc-1].propOne);

// can't using for loop: TypeError: Cannot read property 'propOne' of undefined
for (i = 0; i < 10; i++)
console.log(doc.acc[lengthAcc - i].propOne);




);









share|improve this question






















  • Why to 10? AFAIK Mongoose does not do negative notation on lengths

    – Neil Lunn
    Mar 7 at 4:33












  • Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

    – ceg
    Mar 7 at 4:39











  • i=1, start i from 1

    – Şivā SankĂr
    Mar 7 at 4:41











  • I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

    – Neil Lunn
    Mar 7 at 4:53












  • Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

    – ceg
    Mar 7 at 4:55














0












0








0








I'm new to js (clearly). I'm using mongoose and I was wondering : why do I get an error while accessing property with a for loop?



I'm using a schema that contains embedded schema



Usr.findOne(numCli: req.body.numTar, function(err, doc)

if (doc)

var i = 0;
const lengthAcc = doc.acc.length;
//can access prop individually
console.log(doc.acc[lengthAcc-1].propOne);

// can't using for loop: TypeError: Cannot read property 'propOne' of undefined
for (i = 0; i < 10; i++)
console.log(doc.acc[lengthAcc - i].propOne);




);









share|improve this question














I'm new to js (clearly). I'm using mongoose and I was wondering : why do I get an error while accessing property with a for loop?



I'm using a schema that contains embedded schema



Usr.findOne(numCli: req.body.numTar, function(err, doc)

if (doc)

var i = 0;
const lengthAcc = doc.acc.length;
//can access prop individually
console.log(doc.acc[lengthAcc-1].propOne);

// can't using for loop: TypeError: Cannot read property 'propOne' of undefined
for (i = 0; i < 10; i++)
console.log(doc.acc[lengthAcc - i].propOne);




);






node.js mongoose nested schema






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 4:30









cegceg

32




32












  • Why to 10? AFAIK Mongoose does not do negative notation on lengths

    – Neil Lunn
    Mar 7 at 4:33












  • Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

    – ceg
    Mar 7 at 4:39











  • i=1, start i from 1

    – Şivā SankĂr
    Mar 7 at 4:41











  • I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

    – Neil Lunn
    Mar 7 at 4:53












  • Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

    – ceg
    Mar 7 at 4:55


















  • Why to 10? AFAIK Mongoose does not do negative notation on lengths

    – Neil Lunn
    Mar 7 at 4:33












  • Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

    – ceg
    Mar 7 at 4:39











  • i=1, start i from 1

    – Şivā SankĂr
    Mar 7 at 4:41











  • I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

    – Neil Lunn
    Mar 7 at 4:53












  • Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

    – ceg
    Mar 7 at 4:55

















Why to 10? AFAIK Mongoose does not do negative notation on lengths

– Neil Lunn
Mar 7 at 4:33






Why to 10? AFAIK Mongoose does not do negative notation on lengths

– Neil Lunn
Mar 7 at 4:33














Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

– ceg
Mar 7 at 4:39





Should have clarifed. Each array has 10 objects in it. Just thought I could use a for loop to access each prop of each individual object

– ceg
Mar 7 at 4:39













i=1, start i from 1

– Şivā SankĂr
Mar 7 at 4:41





i=1, start i from 1

– Şivā SankĂr
Mar 7 at 4:41













I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

– Neil Lunn
Mar 7 at 4:53






I don't think you have 10 elements in what you are checking. console.log(doc.acc[-1].propOne) will reproduce the error. Instead your loop should be for (i = 1; i < lengthAcc; i++) to ensure you are not ever using a negative notation. Note these are not "really" arrays, but complex objects which just have "array like accessors". Hence the difference.

– Neil Lunn
Mar 7 at 4:53














Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

– ceg
Mar 7 at 4:55






Thanks. Should have seen it, yet it had eluded me for quite come time. Definitely need a break. And thanks for the clarification regarding those "arrays". Will look into it

– ceg
Mar 7 at 4:55













1 Answer
1






active

oldest

votes


















0














If check your code then in first console



 console.log(doc.acc[lengthAcc-1].propOne);


you are try to access doc.acc[lengthAcc-1].propOne so it's work



but in second console



console.log(doc.acc[lengthAcc - i].propOne);


you are try to access doc.acc[lengthAcc-i].propOne and i=0 so it's doc.acc[lengthAcc-0].propOne that's why it's not work



bcz array.length return number of element in array,
so if array contain 5 element (0-4) so array.length return 5



and if you try to access array[array.length - 1] = 4 it's equal to array[4] so it's work



but if you try to access array[array.length - 0] = 5 it's equal to array[5] and array doe not have index 5 so it throw error.



So, the solution is just start for loop from 1



for (i = 1; i < 10; i++) 
console.log(doc.acc[lengthAcc - i].propOne);






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',
    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%2f55036112%2faccess-property-of-object-nested-in-array-nested-in-object-with-a-for-loop-mongo%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














    If check your code then in first console



     console.log(doc.acc[lengthAcc-1].propOne);


    you are try to access doc.acc[lengthAcc-1].propOne so it's work



    but in second console



    console.log(doc.acc[lengthAcc - i].propOne);


    you are try to access doc.acc[lengthAcc-i].propOne and i=0 so it's doc.acc[lengthAcc-0].propOne that's why it's not work



    bcz array.length return number of element in array,
    so if array contain 5 element (0-4) so array.length return 5



    and if you try to access array[array.length - 1] = 4 it's equal to array[4] so it's work



    but if you try to access array[array.length - 0] = 5 it's equal to array[5] and array doe not have index 5 so it throw error.



    So, the solution is just start for loop from 1



    for (i = 1; i < 10; i++) 
    console.log(doc.acc[lengthAcc - i].propOne);






    share|improve this answer



























      0














      If check your code then in first console



       console.log(doc.acc[lengthAcc-1].propOne);


      you are try to access doc.acc[lengthAcc-1].propOne so it's work



      but in second console



      console.log(doc.acc[lengthAcc - i].propOne);


      you are try to access doc.acc[lengthAcc-i].propOne and i=0 so it's doc.acc[lengthAcc-0].propOne that's why it's not work



      bcz array.length return number of element in array,
      so if array contain 5 element (0-4) so array.length return 5



      and if you try to access array[array.length - 1] = 4 it's equal to array[4] so it's work



      but if you try to access array[array.length - 0] = 5 it's equal to array[5] and array doe not have index 5 so it throw error.



      So, the solution is just start for loop from 1



      for (i = 1; i < 10; i++) 
      console.log(doc.acc[lengthAcc - i].propOne);






      share|improve this answer

























        0












        0








        0







        If check your code then in first console



         console.log(doc.acc[lengthAcc-1].propOne);


        you are try to access doc.acc[lengthAcc-1].propOne so it's work



        but in second console



        console.log(doc.acc[lengthAcc - i].propOne);


        you are try to access doc.acc[lengthAcc-i].propOne and i=0 so it's doc.acc[lengthAcc-0].propOne that's why it's not work



        bcz array.length return number of element in array,
        so if array contain 5 element (0-4) so array.length return 5



        and if you try to access array[array.length - 1] = 4 it's equal to array[4] so it's work



        but if you try to access array[array.length - 0] = 5 it's equal to array[5] and array doe not have index 5 so it throw error.



        So, the solution is just start for loop from 1



        for (i = 1; i < 10; i++) 
        console.log(doc.acc[lengthAcc - i].propOne);






        share|improve this answer













        If check your code then in first console



         console.log(doc.acc[lengthAcc-1].propOne);


        you are try to access doc.acc[lengthAcc-1].propOne so it's work



        but in second console



        console.log(doc.acc[lengthAcc - i].propOne);


        you are try to access doc.acc[lengthAcc-i].propOne and i=0 so it's doc.acc[lengthAcc-0].propOne that's why it's not work



        bcz array.length return number of element in array,
        so if array contain 5 element (0-4) so array.length return 5



        and if you try to access array[array.length - 1] = 4 it's equal to array[4] so it's work



        but if you try to access array[array.length - 0] = 5 it's equal to array[5] and array doe not have index 5 so it throw error.



        So, the solution is just start for loop from 1



        for (i = 1; i < 10; i++) 
        console.log(doc.acc[lengthAcc - i].propOne);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 4:49









        Tausif ShaikhTausif Shaikh

        696




        696





























            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%2f55036112%2faccess-property-of-object-nested-in-array-nested-in-object-with-a-for-loop-mongo%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

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович