Select 在 Node jS 和 MongoDB 中不同

Select Distinct in Node jS and MongoDB

我有一个问题,我希望能够获取与教室相关的所有数据而不重复。我的代码看起来像这样。问题是在控制台中数据正确显示,没有重复。但是在站点中没有数据显示。

这是控制台中的数据显示

[ '6 Usaha', '6 bakti' ]

时间表架构

var TimetableSchema = new mongoose.Schema ({

    timeslot: {
        required: true, 
        'type': Number,

    },

    classroom :{
        type:String, 
        required: true, 
    },

    subject :{
        type:String, 
        required: true, 
    },

    teacher :{
        type:String, 
        required: true, 
    },

    year :{
        type:String, 
        //required: true, 
    },

    session :{
        type:Number,
        //required:true,
    }


})

var Timetable = mongoose.model('Timetable', TimetableSchema);
module.exports = Timetable;

路线

router.get('/timetable',mid, function(req,res){
  Timetable.distinct("classroom",function(err,timetable){
    if (err) throw err;
    console.log(timetable);
    res.render('admin_content/timetable',{'timetable':timetable});
  });
});

观看次数

<tbody>
   <% timetable.forEach(function (timetable) { %>
   <tr>
   <td><%= timetable.classroom %></td>
   <td><a class="button" href="/subject/edit/<%= timetable.classroom %>"</a>View&nbsp;</td>
   </tr>
   <% }) %>
</tbody>

像下面这样更改您的代码,因为您的数组不包含 js 对象,它只是 primitive[ '6 Usaha', '6 bakti' ]

<tbody>
   <% timetable.forEach(function (value) { %>
   <tr>
   <td><%= value %></td>
   <td><a class="button" href="/subject/edit/<%= value %>"</a>View&nbsp;</td>
   </tr>
   <% }) %>
</tbody>