How to dynamically create a bootstrap dropdown based on data
<p>So I am currently attempting to build a shopping cart application using the MEAN stack (mongodb/mongoose, express, ajax, node.js). Using bootstrap to build the site. </p>

<p>There are groups of products that can be created or destroyed so need to be dynamically created. I have a collection in my database called groups. It contains on field(groupName: 'movies') and than the id that mongo creates for it. So I need to go through the groups collection and for each group I find create a dropdown option for that group.</p>

<p>This is my controller for the page </p>

<pre><code>//bring in models of data
var products = require('../models').Products;
var groups = require('../models').Groups;

//page functions go inside of module.exports
module.exports = {
    index: function(req, res){
        products.find({}, function(err, products){
            if(err){
                consol.log(err);
            }
            else{
                res.render('basic-page1', {dropdown: dropdown, title: 'Home', beforelogin: true, subtitle: 'Home', underheaderp: 'To search for products select a product group.'});

            }
        });
    }
}

var dropdown = function createSelect(){
    var dd = "";
    dd+= "&lt;button class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown' id='dropdownProduct'&gt;Please Select a Product Group&lt;span class='caret'&gt;&lt;/span&gt;&lt;/button&gt;";
    var group = groups.find({}, function(err, groups){
        if(err){
            console.log(err);
        }
        else{
            dd+="&lt;ul class='dropdown-menu col-xs-5'&gt;";
            var i = 0;
            while(i &lt;= group.length){
                dd+="&lt;li&gt;&lt;a href='#'&gt;Hockey Jerseys&lt;/a&gt;&lt;/li&gt;";
            }
                dd+="&lt;/ul&gt;";
        }
    });
    return dd;
}
</code></pre>

<p>This code does not get any errors but does not print any of the dropdown options. The wile loop is were the problems are occurring. </p>

<p>When I change that loop to while(i &lt; groups.length){..} or to while(i &lt; 2){..} for testing purposes I get an error of
RangeError: Invalid string length</p>

<p>With further research of this error it does not seem to be very clear what exactly may be occurring.</p>

<p>Am I missing a step in the attempt to loop through the groups? </p>

<p>Any help/guidance is greatly appreciated.</p>