0

From my Node app I am trying to send a JSON array of MongoDB collection to client side HTML/AJAX. I want the Node.js data to reflect on a <select> or on a dropdown. The Node.js response object is sending 2 arrays hostArr and portArr I wish to concatenate the adjacent values and display it on select options. I have included my progress below, this for obvious reason doesn't work. Looking for a solution here..

app.js

MongoClient.connect(url,{ useUnifiedTopology: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("mongoDetails");
    dbo.collection("mongoDetails").find({}).toArray(function(err, result) {
      if (err) throw err;
      var hostArr =[];
      var portArr =[];
      for(i=0;i<result.length;i++){
        hostArr.push(result[i].hostname);    
      }
      for(i=0;i<result.length;i++){  
        portArr.push(result[i].port);   
      }
      console.log(hostArr);
      console.log(portArr);
      res.json({mongoDet1:hostArr, mongoDet2:portArr});
      db.close();
    });
  });

Index.html

<div >
    <select id="mongoDetails">
        <option value="1"></option>
        <option value="2"></option>
        <option value="3"></option>
    </select>
</div>


<script type="text/javascript">
    $(document).ready(function(){   
        $.ajax({
            url: "http://localhost:8070/api/route1",
            type: 'POST',
            dataType:'json',                    
            success: function(res) {
                console.log(res);
                $.each(res, function(key, value) {
                console.log(key);
                console.log(value);
                $("#mongoDetails").value(res.mongoDet1[value]+res.mongoDet2[value]);   
            });
            }
        });    
    });                           
</script>

1 Answer 1

2

To do this, you can use the following method:

MongoClient.connect(url,{ useUnifiedTopology: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("mongoDetails");
    dbo.collection("mongoDetails").find({}).toArray(function(err, result) {
      if (err) throw err;

      var resultArr =[];

      for(i=0;i<result.length;i++){
        resultArr.push({
          hostname: result[i].hostname,
          port: result[i].port
        });    
      }
      console.log(hostArr);
      res.json({content:resultArr});
      db.close();
    });
  });

<div>
    <select id="mongoDetails"></select>
</div>


<script type="text/javascript">
    $(document).ready(function(){   
        var selectElement = document.getElementById('mongoDetails');
        $.ajax({
            url: "http://localhost:8070/api/route1",
            type: 'POST',
            dataType:'json',                    
            success: function(res) {
                console.log(res);
               res.content.forEach(item => {
                 console.log(item);
                   selectElement.options[selectElement.options.length] = new Option(item.hostname, item.port);
              });
            }
        });    
    });                           
</script>

This tutorial shows exactly what you need to do


also you can get <select> value with this code:

var value = document.getElementById("mongoDetails").value;
console.log(value);
Sign up to request clarification or add additional context in comments.

7 Comments

The console.log() gives both arrays yet I don't see the values being reflected on <select> options
That is, the response sample is something like this { mongoDet1:['name1','name2'], mongoDet2:[25,79] }
Included the console log image for easier understanding
Now, I want the select options to be like localhost/27017, localhost/27018, localhost/27019... and so on
I corrected the answer again. I think the problem is solved
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.