如何基于其他选择创建过滤器下拉列表

时间:2019-07-09 12:31:10

标签: javascript node.js mongodb ejs

我想在ejs中的另一个选择上创建过滤器下拉列表,这两个列表都来自mongo数据库。我该怎么办? 我正在使用节点js

<label>Gender</label>
<select placeholder="Enter Your Gender">
  <option selected>Select Your Gender</option>
  <option>Male</option>
  <option>Female</option>
</select>
<label>Title </label>
<select name="title" placeholder="Enter Your Title">
  <%for(let t of title){%> //we can write(var ms = 0;ms< status.length; i++)instead of (let ms of maritalstatus)
  <option value="<%= t.male_english%>"><%= t.male_english%></option>
  <%}%>
</select>

app.js

app.get('/data-en', function(req, res, next) {
  let data = {} // data will send to page
  // get data from database
  db.collection('religious_category').find({}).toArray().then(religious => {
    data.religious = religious
    db.collection('list_of_marital_status').find({}).toArray().then(maritalstatus => {
      data.maritalstatus = maritalstatus

      db.collection('list_of_countries').find({}).toArray().then(countries => {
        data.countries = countries
        db.collection('honorifi_Title').find({}).toArray().then(title => {
          data.title = title

          res.render('data-en', data) // at the end
        })
      })
    })
  })
})

app.post('/data-en', (req, res, next) => {
  db.collection('newperson-en').insertOne(req.body, () => {
    res.redirect('/data-en')
  })
})
我在数据库中有男性和女性的数据列表 我要按性别,因为男性的头衔出现了男性头衔,反之亦然

1 个答案:

答案 0 :(得分:0)

您正在将名为data的对象传递到模板,并且尝试访问名为title的对象。

您必须将对象的属性直接传递给模板:

res.render('data-en', {
        title: data.title,
        religious: data.religious,
        maritalstatus: data.maritalstatus,
        countries: data.countries
    });