如何在mongoose中显示ejs中的特定数据?

时间:2016-11-14 17:42:02

标签: node.js mongodb mongoose ejs mongoose-schema

我是NodeJS和MongoDB的新手。我想使用mongoose从我的数据库中获取特定数据。这些特定数据由searchIndex和searchValue决定,它作为post请求提供 提供searchIndex和searchValue

的ejs页面
<form action="/searchDetails" method="post">
 <table>
 <tr>
 <td>
   Select Search Category:
 </td>
 <td>
  <select name="searchIndex">
    <option>Name</option>
    <option>Rollno</option>
    <option>Branch</option>
    <option>Hostel</option>
    <option>RoomNo</option>
    <option>ContactNo</option>
    <option>Semester</option>
    <option>FName</option>
    <option>MName</option>
    <option>Address</option>
    </select>
   </td>
 </tr>
 <tr>
  <td>Enter the Search Value:</td>
  <td><input type="text" name="searchValue"></td>
 </tr>
 <tr>
   <td><input type="Submit" name="Search"></td>
 </tr>
 </table>
 </form>

这是我的猫鼬模式      var mongoose = require(&#39; mongoose&#39;);

 var userSchema = new mongoose.Schema({
 username: String,
 password: String, 
 });
 var studentSchema=new mongoose.Schema({
    Name:String,
    Rollno:String,
    Branch:String,
    Hostel:String,
    RoomNo:String,
    ContactNo:String,
    Semester:String,
    FName :String,
    MName:String,
    Address:String,

    });
    mongoose.model('Students3', studentSchema);

     mongoose.model('User', userSchema);

这是我的server.js

   router.post('/searchDetails', function(req, res, next) {
   var searchIndex=req.body.searchIndex;
   var searchValue=req.body.searchValue;
   students.find({searchIndex:searchValue},function(err,data){
   if(err)
   {
    console.log(err);
   }
   else if(!data)
   {
    console.log('no data');
    res.redirect('/homepage');
   }
   else
   {
    console.log(data);
    output=data;
    res.redirect('/editStudents')
    router.get('/editStudents',function(req,res,next){
    res.render('editStudents',{output});
    });
   }
   });
   });

这是我的ejs,我显示值

     <table border="1">
     <tr>
     <td>
      <i>Name</i>
      </td>
      <td>
      <i>Rollno</i>
      </td>
      <td>
      <i>Branch</i>
      </td>
      <td>
       <i>Hostel</i>
       </td>
       <td>
       <i>RoomNo</i>
       </td>
       <td>
        <i>ContactNo</i>
       </td>
       <td>
        <i>Semester</i>
       </td>
       <td>
     <i>Father's Name</i>
    </td><td>
    <i>Mother's Name</i>
    </td><td>
    <i>Address</i>
    </td>
    </tr>
    <%
    output.forEach(function(name)
    {
     %>
     <tr>
     <td><%=name.Name%></td>
     <td><%=name.Rollno%></td>
     <td><%=name.Branch%></td>
     <td><%=name.Hostel%></td>
    <td><%=name.RoomNo%></td>
    <td><%=name.ContactNo%></td>
    <td><%=name.Semester%></td>
    <td><%=name.FName%></td>
    <td><%=name.MName%></td> 
    <td><%=name.Address%></td> 
   </tr>
    <%
    })
    %>
    </table>

但问题是它没有显示任何数据。请帮助

0 个答案:

没有答案