Javascript 提交帖子并在Nodejs/EJS中处理结果

Javascript 提交帖子并在Nodejs/EJS中处理结果,javascript,node.js,mongodb,express,ejs,Javascript,Node.js,Mongodb,Express,Ejs,我有一个索引站点(EJS),它将数组读入select表单 index.ejs <html> <head> </head> <body> <center> <form action="/dates" method="POST"> <b>INDEX:</b> <select id="index"

我有一个索引站点(EJS),它将数组读入select表单

index.ejs

<html>
  <head>
  </head>
 <body>
    <center>        
        <form action="/dates" method="POST">
            <b>INDEX:</b>
                <select id="index" name="index" onchange="this.form.submit()">
                    <option id="0"> Please choose Index </option>
                    <% collectionnames.map(item=> { %>
                    <option id="<%= item._id%>"> <%= item %> </option>
                    <% }) %>
                    </select>
                <br>
        </form>
 </body>
</html>
如何将数组“unique”传递回index.ejs,以另一种select形式处理它?现在发生的情况(使用res.send(unique))是,我在浏览器中收到一个包含数组值的列表:

0   "2018-09-01"
1   "2018-09-02"
2   "2018-09-05"
3   "2018-09-03"
4   "2018-09-04"
5   "2018-08-31"
更具体:如何将数组/值检索回my index.ejs并对其进行处理


谢谢。

您可以使用
进行
集合名称上的
循环,如:

//Assuming your `collectionnames = ['2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04']`

<select id="selectId">
    <option value="">Select</option>
    <% if (collectionnames.length) { %>
        <% for (var i = 0; i < collectionnames.length; i++) { %>
            <option value="<%=collectionnames[i]%>"><%=collectionnames[i] %> </option>
        <% } %>
    <% } %>
</select>

纳文,谢谢!这首先让我走得更远,因为它是我想要的(!),其次它为我指明了一个方向。。。。。这项改变如预期的那样奏效。@MaikelNight我很高兴能帮助你。如果您需要,请在此处或github发布更多查询,以便进行更改
//Assuming your `collectionnames = ['2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04']`

<select id="selectId">
    <option value="">Select</option>
    <% if (collectionnames.length) { %>
        <% for (var i = 0; i < collectionnames.length; i++) { %>
            <option value="<%=collectionnames[i]%>"><%=collectionnames[i] %> </option>
        <% } %>
    <% } %>
</select>
app.post('/dates', function (req, res) {
    const postBody = req.body;
    index = postBody.index;
    var dates = [];
    MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true }, function (err, client) {
        if (err) {
            console.log("Error");
        } else {
            console.log("Connected to fetch results");
        }
        var db = client.db('my-mongodb');
        db.collection (postBody.index, function(err, values){
            values.find().sort({VALUETIME:+1}).toArray( function (err,results){
                results.forEach(function(result){       
                    dates.push(result.VALUEDATE);
                    //console.log(elem)
                });
                console.log("got results...")
                function onlyUnique(value, index, self) { 
                    return self.indexOf(value) === index;
                }
                var unique = dates.filter( onlyUnique );
                console.log(unique);

                // Change here
                res.render('index.ejs' , { collectionnames: unique });
        });
    });
});
});